Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created November 10, 2017 07:39
Show Gist options
  • Save Shuumatsu/323aee81201403b54b403283844b5e2e to your computer and use it in GitHub Desktop.
Save Shuumatsu/323aee81201403b54b403283844b5e2e to your computer and use it in GitHub Desktop.
用 generator 实现 async/await
const gFoo = function* () {
const a = yield Promise.resolve(1)
const b = yield Promise.resolve(2)
const c = yield Promise.resolve(3)
const d = yield Promise.resolve(4)
return [a, b, c, d]
}
const co = genF => (...args) => {
const gen = genF(...args)
const h = toPass => {
const { done, value } = gen.next(toPass)
return done ?
value :
value.then(
resolvedValue => h(resolvedValue)
)
}
return h()
}
co(gFoo)().then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment