Interview question: javascript implement async await
Oct 11, 2021
asyncAwaitImpl.js
// see https://segmentfault.com/a/1190000022638499var async = (gen) => {
var g = gen()
p = g.next().value
var next = (p) => {
p.then((v) => {
var p2 = g.next(v).value
if (p2) {
next(p2)
}
})
}
next(p)
}
async(function* () {
console.log(yield /*yield like await*/ new Promise((resolve) => {
resolve("1")
})) console.log(yield new Promise((resolve) => {
resolve("2")
}))
})
If you like my article, donate me a coffee and let me continue