3 ms·
It blocks the logic flow of the current function, but in a way that can be resumed later If it helps you think about it, any await statement can be converted s
by brundolf 3y ago
It blocks the logic flow of the current function, but in a way that can be resumed later
If it helps you think about it, any await statement can be converted straightforwardly to an old-fashioned promise resolution with a callback:
async function foo1() {
doStuff()
const res = await doIO()
return 'Result: ' + res
}
function foo2() {
doStuff()
return doIO().then(res => {
return 'Result: ' + res
})
}
- TeMPOraL 3y agoThat's what I thought. But then, unless foo2() is an async function, the entire thread that's running foo2() will be blocked, will it not?
- brundolf 3y agoNope. It returns a promise, which other logic can then hang a callback off of. But the promise is returned immediately (if just resolves and calls the callback later, like any event listener would)