Awaiting thenables

# Francisco Tolmasky (9 years ago)

Out of curiosity, in ES7, would the following code:

console.log(await { then: function(x) { x(5) } })
console.log(6)

Print out 5 then 6, or still 6 then 5? In other words, is the asynchronousness gauranteed by the await, or by the underlying Promise implementation?

# Francisco Tolmasky (9 years ago)

Apologies, I’m aware the last example didn’t make sense. What I meant was more along the lines of the following:

setImmediateOrOtherNextRunLoopMethod(function() { console.log(6) })
console.log(await { then: function(x) { x(5) } })

In other words, would the “then” fire on the same run loop (since it isnt a fancy Promise), or still have the underlying await engine (step function) make sure it takes place on the next iteration (and in this case thus possibly make it show up after the 6 instead of beefore).

# Mark S. Miller (9 years ago)

It would be postponed to a later job, i.e., turn of the event loop.

# Jordan Harband (9 years ago)

so, in other words, await thenable would wrap the thenable in Promise.resolve, which would ensure it fires on the next tick?

# Mark S. Miller (9 years ago)

Y

# Brian Terlson (9 years ago)

Yes, at least the current spec literally just calls Promise.resolve on any awaited value. See step 12: tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-start. So it’s identical semantics to Promise.resolve(thenable).