Skip to content

Instantly share code, notes, and snippets.

@lili21
Last active August 14, 2017 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lili21/dd4786b6c8b49c6d61b3f13e1fbdf7fc to your computer and use it in GitHub Desktop.
Save lili21/dd4786b6c8b49c6d61b3f13e1fbdf7fc to your computer and use it in GitHub Desktop.
generator, promise.
function co (_g) {
const ge = _g()
next(ge)
}
function next(ge, _v) {
const { value, done } = ge.next(_v)
if (done) {
ge.next(value)
} else {
if (typeof value.then === 'function') {
value.then(result => {
next(ge, result)
}).catch(e => {
ge.throw(e)
})
} else {
next(ge, value)
}
}
}
co(function *() {
try {
const a = yield Promise.resolve(1)
const b = 2
console.log(a, b)
const c = yield Promise.reject(3)
console.log(a + b + c)
} catch (e) {
console.log('catchchchc')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment