2 ways to implement async await in Javascript

  1. Using Promises
    • async function always returns a promise, and await waits for that promise to resolve.
    • Hence, async await can be compiled to Promises and made to work on ES5 and below
  2. Using Generators
    • Another, more natural way to implement async await is to use ES6 Generators. Technically async-await are just syntactic sugar on Generators.
    • Generators allow functions to return midway and then resume execution later. Hence, these can be used to implement async await functionality
    • But, Typescript doesn’t support downcompiling Generators to ES5 and so for async await support with ES5 target, Generators are out of question. Instead TS compiles to promises (ref).
    • For ES6 target, TS compiles async await to generators. (ref)
    • But, Typescript 2.3 will support Generator compilation to ES5 and so post May 2017, this will be possible…

TODO

  • What approach do JS interpretors like V8 use to handle async await. My money is on Generators
  • What’s interesting to see is which approach is more performant:
    • Per fast-async (‘fast-async’ is a Babel v6.x.x plugin that implements the ES7 keywords async and await using syntax transformation at compile-time, rather than generators.), it’s 3-4 times faster in a browser/node, and as much as 10 times faster on a mobile browsers, mainly due to avoiding generators (and therefore regenerator) and compiling async  await to promises directly.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s