Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Proposal for ECMAScript language feature: asynchronous "do expressions"

Notifications You must be signed in to change notification settings

inexorabletash/ecmascript-async-do-expressions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Asynchronous "do expressions"

Overview

Building on async functions and the not-formally-proposed "do expressions", "async do expressions" let you write asynchronous blocks in an expression context, returning the completion value (a Promise).

This is useful when functions expect an asynchronous result (a Promise) as input, and the steps to produce that result can be expressed in line with the function call.

Example:

fetchEvent.respondWith(async {
  var response = await fetch('https://example.com/resource');
  var body = await response.text();
  'BEGIN---\n' + body + '\n---END\n';
});

This is similar to an "immediately invoked async function expression", i.e.:

fetchEvent.respondWith((async () => {
  var response = await fetch('https://example.com/resource');
  var body = await response.text();
  return 'BEGIN---\n' + body + '\n---END\n';
})());

Or more verbosely:

async function f() {
  var response = await fetch('https://example.com/resource');
  var body = await response.text();
  return 'BEGIN---\n' + body + '\n---END\n';
};
let p = f();
fetchEvent.respondWith(p);

Syntax

PrimaryExpression :
  ...
  AsyncBlockExpression
  
AsyncBlockExpression :
  "async" [no LineTerminator here] Block
  
ExpressionStatement :
  [lookahead ∉ {{, function, class, let [, async}] Expression;

Semantics

TODO - static and runtime semantics

  • var hoisting behavior needs definition
  • break, continue and return need definition

References

About

Proposal for ECMAScript language feature: asynchronous "do expressions"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published