在多層嵌套回調時,寫完的代碼層次過多,很難進行維護和二次開發promise
let state = 1; function step1(resolve, reject) { console.log("1.洗菜作飯"); if (state == 1) { resolve("洗菜作飯完成") } else { reject("洗菜作飯--出錯") } } function step2(resolve, reject) { console.log("2.坐下來吃飯"); // state = 3; if (state == 1) { resolve("坐下來吃飯完成") } else { reject("坐下來吃飯--出錯") } } function step3(resolve, reject) { console.log("3.收拾桌子洗碗"); if (state == 1) { resolve("收拾桌子洗碗完成") } else { reject("收拾桌子洗碗--出錯") } } new Promise(step1).then( function (val) { console.log(val); return new Promise(step2) } ).then(function (val) { console.log(val); return new Promise(step3) }).then( function (val) { console.log(val); return val } )
state = 3spa