Async Immediately Invoked Function Expression (iife) syntax simplificaton

I have this idea - I'm not sure if it's possible to actually implement, but I still want it to suggest

So basically if I'm not in async function context, and I have to make multiple async function calls in specific order, I have two options:



(async function() {
    // ... await asyncFunction() ...

I'm not a big fan of .then(), especially if there are two or three callbacks in a chain, so I would almost always stick with second option.
So, my suggestion is to have this syntactic sugar for that option, like this:

async {
    // ... await asyncFunction() ...

The drawback I'm aware of - it will be impossible to give a name for such "anonymous function", like that:

(async function usefulNameForDebugging() {
    // ... await asyncFunction() ...

it's very useful for debugging purposes

PS I'm aware of top-level await thing, but it I'm mostly working with JS frameworks, so I'm not actually controlling code execution, I'm facing this need to wait for async calls not on the top-level

I think you would be interested in GitHub - tc39/proposal-async-do-expressions: async `do` expressions for JavaScript

1 Like