Here is an example how await
operator can be desugared into Promise-based code without ES6 generators (and state machines that emulate generators missing in ES5).
The only assumption is that there is Promise.resolve
(to create an empty promise for the loop finalization) among the globals.
Examples are written with "throw early" policy adoption: if promisified code has a synchronous preamble that throws we prefer to throw instead of creating rejected Promise.
Note that patterns below don't cover control flow breaks like return
, break
, continue
and throw
(for these breaks we should use a bit more complex structures).