可以把以下程式碼複製到html檔案中用chrome執行,或者用nodejs執行
function async1(chain) { setTimeout(function(){ chain.next('async1') //2.恢復async1的等待 }, 1300); } function async2(param,chain) { setTimeout(function(){ chain.next(param + ' async2') //4.恢復async2的等待 }, 1100); } var chain; function *chainWrapper() { var a = yield async1(chain); //1.等待async1完成執行 var b = yield async2(a,chain); //3.等待async2完成執行 console.log(b);//async1 async2 } chain = chainWrapper(); chain.next();