You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionrun(gen){returnnewPromise(function(resolve,reject){if(typeofgen=="function")gen=gen();// 如果 gen 不是一个迭代器if(!gen||typeofgen.next!=="function")returnresolve(gen);onFulfilled();functiononFulfilled(res){varret;try{ret=gen.next(res);}catch(e){returnreject(e);}next(ret);}functiononRejected(err){varret;try{ret=gen.throw(err);}catch(e){returnreject(e);}next(ret);}functionnext(ret){if(ret.done)returnresolve(ret.value);varvalue=toPromise(ret.value);if(value&&isPromise(value))returnvalue.then(onFulfilled,onRejected);returnonRejected(newTypeError("You may only yield a function, promise "+'but the following object was passed: "'+String(ret.value)+'"'));}});}functionisPromise(obj){return"function"==typeofobj.then;}functiontoPromise(obj){if(isPromise(obj))returnobj;if("function"==typeofobj)returnthunkToPromise(obj);returnobj;}functionthunkToPromise(fn){returnnewPromise(function(resolve,reject){fn(function(err,res){if(err)returnreject(err);resolve(res);});});}module.exports=run;
自动执行
原文链接:ES6 系列之 Generator 的自动执行
The text was updated successfully, but these errors were encountered: