Problem
While coding, it is frequent to forgot the await
keyword before returning a Promise.
This leads to unexpected results:
- The Promise is executed, but your code is now desynchronized
- You test the result, expect null of false, but the Promise is evaluated to true, so your test fails
It can take ages before you actually realize what is happening.
In addition, writing async / await everywhere in your code is manual and painful.
Async should be the default mode for modern javascript.
We should be able to opt-in : async by default.
Proposal
Let the developer specify it wants its code to be async by default, by adding a 'use async';
instruction to the EcmaScript specification.
'use strict';
'use async';
When used, all functions are considered as async and are expected to return a Promise. Legacy browser functions are converted to promises if needed, but that should not be necessary since await 42
always fullfills to 42.
Example 1
const f = async () => {}
await f();
can now be written as :
'use async';
const f = () => {}
f();
Example 2
const f1 = async () => {}
const f2 = async () => {}
await Promise.all([f1, f2]);
can now be written as :
'use async';
const f1 = () => {}
const f2 = () => {}
Promise.all([f1, f2]);
The interesting part is that the 'use async';
instruction can be automatically added by bundlers, so you are guaranted your Promises will work as intented.
Main benefits:
- Peace of mind
- Modern Javascript by default
- MicroTasks by default
- WebPerf by default