0

Usually, Promises chains can be rewritten using async/await

   const f = () => Promise.resolve(1).then(a => a+1)

can be translated to

   const f = async () => {
       const a = await Promise.resolve(1)
       return a+1
    }

If I'm not wrong in Javascript async/await is implemented using coroutines (like generators).

So internally Javascript does not ever translate the async/await code to a promise chain code before execution (do not consider async/await a syntactic sugar).

Looking around I found a transpiler that asserts to change async/await to promises chain. Doing so is taking async/await as a syntactic sugar that is expanded before execution. And doing so states that is even more performant than coroutine implementation.

Now I'm wondering.

Can that transpiler always automatically rewrite every async/await code without any problem? Or there are cases where the transpilation can not automatically give the same result as the coroutine implementation?

Same question in another way. Why was chosen to use coroutine implementation and not transpile to the Promises chain? Because transpilation is not always possible?

Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49
  • all async/await code can be rewritten as a Promise chain - mostly very simply - but sometimes it's not so easy, and the result can look messy - the real question is, if you have async/await code, why would you want it as a Promise chain? – Jaromanda X Sep 21 '22 at 07:39
  • `Javascript async/await is implemented using coroutines (like generators)` ... not sure about that - sure, async/await code can be transpiled (and is so) to such code, but that doesn't mean that a browser that natively supports async/await would need to do such a thing – Jaromanda X Sep 21 '22 at 07:40
  • @JaromandaX form this question I can assume that async/await us Generators and so coroutines https://stackoverflow.com/questions/46908575/async-await-native-implementations . Do you think that all code can be "Automatically" rewritten as a promise chain? Or there are cases in wich such automatica transpiler cannot succeed? – Fabiano Taioli Sep 21 '22 at 08:26
  • Why? it speaks of v8 (not the only JS engine that exists) and is OVER 4 years old - even the accepted answer states `it's subject to continuous change` - does it matter how it's implemented in the engine though? (the correct answer is, no) – Jaromanda X Sep 21 '22 at 08:29
  • My curiosity comes from the linked project nodent, that state that translate async/await to Promise chain has give better performance on all majour JS engines. – Fabiano Taioli Sep 21 '22 at 08:35
  • Well, does it do as advertised? I've converted code from async/await to Promise chain - most of the time it's straight forward - but sometimes the resulting Promise chain is an awful mess to look at - also, how async/await is implemented in an engine has no baring on how one would transpile to promise chain – Jaromanda X Sep 21 '22 at 08:38
  • Well, does it do as advertised? .... my doub too. Well, does it do as advertised? .... my doubt too. Would be great if anyone can confirm or criticize what it states – Fabiano Taioli Sep 21 '22 at 08:49
  • 1
    oh, so you haven't even tried it? What's stopping you? – Jaromanda X Sep 21 '22 at 08:51
  • Have tried it on a small project and worked but: 1) If it works on a project don't means it will works on a general basis. 2) Not have make a real benchmark. Setting up a benchmark would be time consuming. – Fabiano Taioli Sep 21 '22 at 09:10

1 Answers1

1

The best solution for this nowadays is to write the code in TypeScript, with target set to ES5, then every await/async will always be correctly replaced with the corresponding Promise chain.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138