需求:promise执行买菜做饭过程
1.买菜
2.洗菜
3.做饭
4.吃饭
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>Document</title> 8 </head> 9 <body> 10 <h2>需求:promise执行买菜做饭过程</h2> <br> 11 1.买菜 <br> 12 2.洗菜 <br> 13 3.做饭 <br> 14 4.吃饭 <br> 15 </body> 16 <script> 17 let flag =1; 18 function step1(resolve,reject){ 19 console.log("准备去买菜"); 20 if(flag==1){ 21 resolve("买好了") 22 }else{ 23 reject("没菜了") 24 } 25 } 26 27 function step2(resolve,reject){ 28 console.log("洗菜"); 29 if(flag==1){ 30 resolve("洗好了"); 31 }else{ 32 reject("没水"); 33 } 34 } 35 function step3(resolve,reject){ 36 console.log("开始做饭"); 37 if(flag==1){ 38 resolve("做好了"); 39 }else{ 40 reject("煤气没了"); 41 } 42 } 43 function step4(resolve,reject){ 44 console.log("准备吃饭"); 45 if(flag==1){ 46 resolve("真好吃"); 47 }else{ 48 reject("真难吃"); 49 } 50 } 51 function step5(resolve,reject){ 52 console.log("剪刀石头布洗碗"); 53 if(flag==0){ 54 resolve("女朋友洗"); 55 }else{ 56 reject("不洗"); 57 } 58 } 59 new Promise(step1).then((val1)=>{ 60 console.log(val1); 61 return new Promise(step2) 62 }).then((val2)=>{ 63 console.log(val2); 64 return new Promise(step3) 65 }).then((val3)=>{ 66 console.log(val3) 67 return new Promise(step4) 68 }).then((val4)=>{ 69 console.log(val4) 70 return new Promise(step5) 71 }).then((val5)=>{ 72 console.log(val5) 73 }) 74 </script> 75 </html>