asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
var RP = require("request-promise"); var sites = awaitPromise.all([ RP("http://www.google.com"), RP("http://www.apple.com"), RP("http://www.yahoo.com") ])
asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
promise .then((age)=>{ return`Your age is ${age}, so you can meet Cang Laoshi`; }) .then((msg)=>{ console.log(`Congratulations! ${msg}`); }) .then((msg)=>{ console.log("Please contact deshui.wang"); }); 输出:
Congratulations! Your age is 20, so you can meet Cang Laoshi Please contact deshui.wang
我们在then里面 也可以是一个异步操作,那么后面的then 将等待前一个promise完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
promise .then((age)=>{ return`Your age is ${age}, so you can meet Cang Laoshi`; }) .then((msg)=>{ setTimeout(()=>{ console.log(`Congratulations! ${msg}`); },5000); }) .then((msg)=>{ console.log("Please contact deshui.wang"); }); 输出 Please contact deshui.wang Congratulations! Your age is 20, so you can meet Cang Laoshi