Stage 1
Classification: Syntactic Change Semantic Change
Human Validated: KW
Title: async do expressions
Authors: Kevin Gibbons
Champions: Kevin Gibbons
Last Presented: January 2021
Stage Upgrades:
Stage 1: 2021-01-27
Stage 2: NA
Stage 2.7: NA
Stage 3: NA
Stage 4: NA
Last Commit: 2021-02-03
Topics: async others
Keywords: asynchronous expression promise
GitHub Link: https://github.com/tc39/proposal-async-do-expressions
GitHub Note Link: https://github.com/tc39/notes/blob/HEAD/meetings/2021-01/jan-27.md#async-do-expressions
Proposal Description:
ECMAScript proposal: async do
expressions
async do
expressions allow you to introduce an asynchronous context within synchronous code without needing an immediately-invoked async function expression.
This proposal builds off of the do expressions proposal.
This proposal has preliminary spec text.
Motivation
Currently the boundary between synchronous and asynchronous code requires defining and invoking an async
function. In the case that you just want to perform a single operation, that’s a lot of syntax for a relatively primitive operation: (async () => {...})()
. This lets you write async do {...}
instead.
Examples
// at the top level of a script
async do {
await readFile('in.txt');
let query = await ask('???');
// etc
}
Promise.all([
async do {
let result = await fetch('thing A');
await result.json();
},
async do {
let result = await fetch('thing B');
await result.json();
},
]).then(([a, b]) => console.log([a, b]));