async blocks proposal

Functionality:

let prom = async {
  let thing = await fetch("thing");
  await frame();
  await dostuff(thing);
}

Would be equivalent to

let prom = Promise.resolve().then(async () => {
  let thing = await fetch("thing");
  await frame();
  await dostuff(thing);
});

Inspiration: rust async blocks

Usecase: primarily in the service workers waitUntil callback, or other places expecting a promise.

This would allow other uses, and may sync well with making do a statement that can accept a block; eg do async {}.

I'm unsure as to the specifications, but was hoping to gain some interest on it. I'm currently unsure as to whether to use return, yield, break, or continue to produce a value; or to disallow returning from it entirely.

Thank you,

-- zeen3.

4 Likes

You'll want to have a look at https://github.com/zenparsing/proposal-async-block :-) Also see the links in https://github.com/inexorabletash/ecmascript-async-do-expressions, especially the do expressions proposal which has multiple issue discussions about async (do) blocks.

2 Likes

I like this. I think they're adding top level await soon which will make it so you don't have to do the first thing as much. Would be nice if you could use something like this so top level await didn't block the app though.

Yes please!
I so often want to create a Promise using async notation, and end up creating an async function just to call it immediately:

let prom = (async () => { *statements* })();

E.g., when calling Promise.all(), one has to provide a list of promises, and I find it absurd one is obliged to create functions just to use async/await notation.

Why can't we skip the function creation and write just:

let prom = async { *statements* };

The result value of the promise should be given by the 'return' statement.