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.

3 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.

1 Like

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.