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]));