JS es6 如何将两个请求写回调?

有A、B两个promise请求,希望B在A返回结果后才发起,但又不想将B写到A里面去,请问怎么写?
慕标5832272
浏览 625回答 2
2回答

Helenr

functionA(){returnnewPromise((res,rej)=>{setTimeout(()=>{console.log('A')res('sayhifromA')},1000)})}functionB(data){returnnewPromise((res,rej)=>{setTimeout(()=>{console.log('B')console.log(data)res()},1000)})}A().then(data=>B(data))

qq_花开花谢_0

functionresolveAfter2Seconds(x){returnnewPromise(resolve=>{setTimeout(()=>{resolve(x);},2000);});}asyncfunctionadd2(x){vara=awaitresolveAfter2Seconds(20);varb=awaitresolveAfter2Seconds(30);returnx+a+b;}add2(10).then(v=>{console.log(v);//prints60after4seconds.});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript