有A、B两个promise请求,希望B在A返回结果后才发起,但又不想将B写到A里面去,请问怎么写?
2 回答
Helenr
TA贡献1780条经验 获得超4个赞
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
TA贡献1835条经验 获得超7个赞
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.});
添加回答
举报
0/150
提交
取消