All version of babel translate an await
statement to a _asyncToGenerator
call, it obviously has some shortcomings:
- Code size grows dramatically
- Requires the
regeneratorRuntime
library
From my understanding of the syntax I think any await
should be equivalent to a Promise#then
call, so the code below:
try {
let user = await getUser();
console.log(user.name);
}
catch (error) {
console.error(error);
}
is just equivalent to:
let promise$of$getUser$ = getUser();
$promise$of$getUser$.then(
$result$ => console.log($result$),
$error$ => console.error($error$)
);
In this way it is also possible to correctly map multiple await
statements or even a mix of Promise#then
and await
statements to a Promise
chain, so I must missed some cases where pure Promise#then
is not suitable for await
statements.