#code
/**
*
* @param 生成器函数 gen
*/
function run(gen) {
var args = [].slice.call(arguments, 1), it;
it = gen.apply(this, args);
return Promise.resolve()
.then(function handleNext(value) {
var next = it.next(value);
return (function handleRes(next) {
if (next.done) {
return next.value;
}
else {
return Promise.resolve(next.value).then(
handleNext,
function handleErr(err){
return Promise.resolve(it.throw(err)).then(handleRes);
})
}
})(next)
})
}
网友评论