ES学习笔记(16)--promise对象的使用(许诺)
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 = 3
相关推荐
nmgxzm00 2020-11-10
xixixi 2020-11-11
88254251 2020-11-01
MarukoMa 2020-09-02
88234852 2020-09-15
陈旭阳 2020-08-31
whynotgonow 2020-08-19
前端开发Kingcean 2020-07-30
whynotgonow 2020-07-29
bowean 2020-07-08
前端开发Kingcean 2020-07-08
88520191 2020-07-05
前端开发Kingcean 2020-06-27
88481456 2020-06-18
whynotgonow 2020-06-16
88520191 2020-06-13
88520191 2020-06-13
89500297 2020-06-13