简体   繁体   English

如何在 JavaScript 中使用多个子异步请求链接承诺?

[英]How to chain promises with several child asynchronous request in JavaScript?

I have several async functions and I want all of them to run synchronously.我有几个异步函数,我希望它们都同步运行。 So tried doing it using promises but it isn't waiting for the third function to complete.所以尝试使用 Promise 来做,但它没有等待第三个 function 完成。 It returns to loop after asyncDataRequestOne request.它在 asyncDataRequestOne 请求后返回循环。

I searched but I am not able to find examples for chaining promises as below or similar.我进行了搜索,但找不到如下或类似链接承诺的示例。

Here is what I have tried这是我尝试过的

Trial 1试验 1


function three(j){
 return new Promise((resolve,reject)=>{
   asyncDataRequestThree(()=>{
     let k = j + 14; 
     resolve();
   });
 });
}

function two(i){
 return new Promise((resolve, reject)=>{
  asyncDataRequestOne().then(()=>{
   asyncDataRequestTwo().then(()=>{
     let  = i+ 7;
     three(j);
     resolve()
   });
  });
 });
}

function one(){
  //Each loop should run only after function three's resolve
  arr.forEach((i) => {
    two(i);
  }
}

Trial 2试验 2

function async three(j){
   await asyncDataRequestThree(()=>{
     let k = j + 14; 
   });
 });
}

function async two(i){
  await asyncDataRequestOne().then(  async ()=>{
   await asyncDataRequestTwo().then(  async ()=>{
     let  = i+ 7;
     await three(j);
   });
  });
}

function one(){
  //Each loop should run only after function three's resolve
  arr.forEach( async () =>{
    await two(i);
  }
}

Thank you谢谢

Array's forEach loop internally implemented something like this: Array 的forEach循环在内部实现了如下内容:

for (var element in array) {
  callback(element); // assuming callback is the argument of Array.forEach()
}

So if you add async to function declaration, it will just make it return Promise .因此,如果您将async添加到 function 声明中,它只会使其返回Promise Loop will not wait for Promise of the callback to finish, it will wait only for callback itself to finish.循环不会等待callbackPromise完成,它只会等待callback本身完成。 Otherwise Array.forEach() would have to return Promise as well.否则Array.forEach()也必须返回Promise

// assuming asyncDataRequestOne, asyncDataRequestTwo, asyncDataRequestThree will return Promise
async three(j) {
  await asyncDataRequestThree();
  // some kind of operation with j
}

async two(i) {
  await asyncDataRequestOne();
  await asyncDataRequestTwo();
  let j = i + 7;
  await three(j)
}

async function one() {
   for (let i = 0; i < 10; i++) {
      await two(i);
   }
}

your two function will be resolved and returns a promise to your loop hence why i will break the chain and it won't await for your three function to resolve which means if you want your three function to return promise before the next loop one of the possibilities is to not use a promise in your two function your two function will be resolved and returns a promise to your loop hence why i will break the chain and it won't await for your three function to resolve which means if you want your three function to return promise before the next loop one of the可能性是不要在你的两个 function 中使用 promise

 function three(p){ return new Promise((resolve,reject)=>{ k = p + 14,resolve(console.log(k)) }) } function two(i){ return j = i+ 7,three(j) } i=0 function one(){ //Each loop should run only after function three's resolve while(i < 10){ two(i) i++ } } one()

or use async await and your code should look something like this或使用异步等待,您的代码应如下所示

 var promise3=function three(p){ return new Promise((resolve,reject)=>{ k = p + 14,resolve(console.log(k)) }) } var promise2=function two(i){ return new Promise((resolve, reject)=>{ let j = i+ 7; resolve(j) }) } i=0 async function one(){ //Each loop should run only after function three's resolve while(i < 10){ r1= await promise2(i) r2= await(promise3(r1)) i++ } } one()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM