Eliminating IIAAFE from modules and scripts, so you don't have to.
We've all seen the IIAAFE pattern out in the wild:
(async () => {
// Use await in here
})();
And it's slightly more cordial cousin, the IIAMFD:
async function main() {
// Use await in here
}
main();
We see these patterns in the following scenarios:
await
usage but need to create an async
context first.await
in it.Async blocks are a simple syntactic feature that allows us to avoid abstruse patterns in order to convey simple ideas.
async {
let result = await fetch(someURL);
let data = await result.json();
console.log('fetched!');
}
Works in scripts and modules, too!
Here's the grammar:
Statement:
...
AsyncStatement
AsyncStatement :
`async` [no LineTerminator here] `{` StatementList[~Yield, +Await, ~Return] `}`
Note: ReturnStatement is not allowed from the statement list of an async statement.