Co 伪实现
/* ---- co 伪实现 ---- */
/*
* @param {function} gen
*
* 0. --> gen() => g
* 1. --> g.next(res: undefined) --> (yield new Promise...)
* 2. --> ret = g.next(res: undefined) => ret.value: Promise
onFulfilled();
function onFulfilled(res) {
let ret;
try {
ret = g.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
function next(ret) {
if (ret.done) return resolve(ret.value);
return ret.value.then(onFulfilled);
}
}); }
co(function*() { const res1 = yield new Promise(res => setTimeout(res, 2500, 10)); console.log(res1); const res2 = yield new Promise(res => setTimeout(res, 500, res1 + 10)); console.log(res2); const res3 = yield new Promise(res => setTimeout(res, 500, res2 + 10)); console.log(res3); }); ```
-->loading...
还没有人评论...