All async/await code can be translated to Promises, or other constructs. Because this is what transpilation with babel has done.
I has assumed the two paradigms where equivalent and that all promises could be rewritten with async/await. Is this true? Or is it an assumption I need to drop.
For a concrete example I have the following code, which contains a promise. I have not seen a way to translate this code to async/await only.
For context, this Mailbox code is for a demo I have to explain the Actor model in the context of the browser/JavaScript
function Mailbox () {
const messages = []
var awaiting = undefined
this.receive = () => {
if (awaiting) { throw 'Mailbox alread has a receiver'}
return new Promise((resolve) => {
if (next = messages.shift()) {
resolve(next)
} else {
awaiting = resolve
}
})
}
this.deliver = async (message) => {
messages.push(message)
if (awaiting) {
awaiting(messages.shift())
awaiting = undefined
}
}
}