zenparsing / proposal-async-block

Eliminating the IIAAFE so you don't have to
6 stars 0 forks source link

Async blocks for JavaScript

Eliminating IIAAFE from modules and scripts, so you don't have to.

The Idea

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:

Async blocks are a simple syntactic feature that allows us to avoid abstruse patterns in order to convey simple ideas.

Example

async {
  let result = await fetch(someURL);
  let data = await result.json();
  console.log('fetched!');
}

Works in scripts and modules, too!

The Details

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.