1

All version of babel translate an await statement to a _asyncToGenerator call, it obviously has some shortcomings:

  1. Code size grows dramatically
  2. 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.

otakustay
  • 11,817
  • 4
  • 39
  • 43

2 Answers2

2

You can use the other 2 plugins: async-to-generator and async-to-module-method.

There's also an experimental plugin called kneden which does try to do what you are suggesting (async to promise). It's still WIP and doesn't account for all cases and most likely will not be able to.

hzoo
  • 1,334
  • 12
  • 9
2

I think you're overlooking loops:

for (let userId of userIds) {
  const user = await getUser(userId);
  console.log(user);
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    Thanks, after some research I find loops and conditions are possible to handle properly but is really hard, mixes of all these complexities may be the reason not to support translating await directly into Promise – otakustay Apr 10 '16 at 13:53