Last active
August 14, 2017 07:36
-
-
Save lili21/dd4786b6c8b49c6d61b3f13e1fbdf7fc to your computer and use it in GitHub Desktop.
generator, promise.
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
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