`await null` to stay in the same tick?
On Sun, Feb 7, 2016 at 1:38 PM, /#!/JoePea <joe at trusktr.io> wrote:
I'm not sure where's the best place to ask, but if I
await null
in an async function does that guarantee that the following code will execute immediately (control flow will not go anywhere else)?
Absolutely not. The continuation of an await always runs in another turn/job, i.e., it always starts with an empty stack. between the awaiting turn and the resumption at the await point, other jobs will interleave.
Await yields to the event loop unconditionally. This is useful for spreading CPU-bound work across multiple events. You can explicitly await conditionally.
if (guard) { await guard; }
On Sun, Feb 7, 2016 at 1:51 PM, Kris Kowal <kris.kowal at cixar.com> wrote:
Await yields to the event loop unconditionally. This is useful for spreading CPU-bound work across multiple events. You can explicitly await conditionally.
if (guard) { await guard; }
Good example, thanks.
Aah, good to know. With Babel this isn't the case, as await null
doesn't
defer, so I was doing await somethingThatMightBeNull
to possibly defer,
but if (somethingThatMightBeNull) await somethingThatMightBeNull
will be
full proof if Babel fixes that.
(Or, maybe it's Facebook Regenerator's fault, not Babel's)
Joe, if you have a specific example, feel free to file an issue and I can take a look. From what I can see, babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&playground=false&code= (async function fn(){ console.log('start') Promise.resolve().then(() %3D> console.log('tick'))%3B%0A%20%20await%20null%3B%0A%20%20%0A%20%20console.log('end')%3B%0A%7D)()%3B would indicate that Babel always defers with Regenerator as well. Better to have this discussion elsewhere though.
I believe Babel uses a modified Regenerator fork that only differs enough to work with Babel, and is otherwise kept as in sync as possible.
I'm not sure where's the best place to ask, but if I
in an async function does that guarantee that the following code will execute immediately (control flow will not go anywhere else)?