var genAsyncTask2 = (val, time) => { return () => { return new Promise(function (resolve, reject) { let timer = setTimeout(() => { clearTimeout(timer); resolve(val); }, time); }); }} // Task queue var taskArr = [GenAsyncTask2 (' First asynchronous task, time 2 seconds ', 2000), GenAsyncTask2 (' Second asynchronous task, time 2 seconds ', 2000), Time 1 second ', 1000), GenAsyncTask2 (' 3rd Asynchronous task, time 0.8 seconds ', 800), GenAsyncTask2 (' 4th Asynchronous task, time 2.5 seconds ', 2500)]; function *genFun(taskArr){ for(let i = 0 , len = taskArr.length; i < len; i++){ yield taskArr[i]().then(res => { console.log('genSync', res); return res; }); }} /** * Automatically execute next of generate function, And ensure the execution order * @ param generateFn generate function * @ param params * / function needed to perform the generate function parameters generateAutoExec (generateFn,... params) { var g = generateFn(... params); var next = function (res) { if (res.done || typeof res.value == 'undefined') return res.value; res.value.then((data) => { console.log('data', data); next(g.next(data)); }).catch((err) => { console.error(err); next(g.throw(err)); }); } next(g.next()); } generateAutoExec(genFun, taskArr);