Created
November 10, 2017 07:39
-
-
Save Shuumatsu/323aee81201403b54b403283844b5e2e to your computer and use it in GitHub Desktop.
用 generator 实现 async/await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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