Promis.then()
程序员文章站
2022-07-09 19:00:55
1 function cook() { 2 console.log('开始做饭。'); 3 var p = new Promise(function (resolve, reject) { 4 setTimeout(function () { 5 console.log('做饭完毕!'); 6 re ......
1 function cook() { 2 console.log('开始做饭。'); 3 var p = new promise(function (resolve, reject) { 4 settimeout(function () { 5 console.log('做饭完毕!'); 6 resolve('鸡蛋炒饭'); 7 }, 1000); 8 }); 9 return p; 10 } 11 12 function eat(data) { 13 console.log('开始吃饭:' + data); 14 var p = new promise(function (resolve, reject) { 15 settimeout(function () { 16 console.log('吃饭完毕!'); 17 resolve('一块碗和一双筷子'); 18 }, 2000); 19 }); 20 return p; 21 } 22 function wash(data) { 23 console.log('开始洗碗:' + data); 24 var p = new promise(function (resolve, reject) { 25 settimeout(function () { 26 console.log('洗碗完毕!'); 27 resolve('干净的碗筷'); 28 }, 2000); 29 }); 30 return p; 31 } 32 33 cook() 34 .then(eat) 35 .then(wash) 36 .then(function (data) { 37 console.log(data); 38 });
输出:
备注:resolve只能传入一个参数,传入的其它参数,读取到的值为"undefined"。