简体   繁体   English

在 nodejs 中开始下一个循环之前,等待每个 for 循环迭代完成

[英]Wait on each for loop iteration to finish before starting the next in nodejs

I have an array with URLs separated by a comma.我有一个 URL 以逗号分隔的数组。 I can't seem to manage to get the loop to finish each iteration before starting the next.在开始下一次迭代之前,我似乎无法设法让循环完成每次迭代。 This is my code:这是我的代码:

const options = {
  method: 'post',
  headers: {
    'Authorization': 'Basic '+Buffer.from(`${user}:${password}`).toString('base64')
  },
}
for (let url of urls.split(",")) {
  console.log(url);
  await axios.get(url, options)
  .then(response => {
    console.log(url);
    <--Rest of my code-->
  })
}

I can see that the first console.log() runs at once, so the for loop is not waiting for one iteration to finish before starting the next.我可以看到第一个 console.log() 立即运行,因此 for 循环不会在开始下一次迭代之前等待一次迭代完成。

I have tried several different solutions I found here to try and make this async, including:我尝试了几种不同的解决方案,我在这里找到了尝试使这个异步,包括:

  1. Changing the "for" loop to a "for await" loop, which caused a syntax error for a reserved keyword.将“for”循环更改为“for await”循环,这会导致保留关键字的语法错误。
  2. Putting the For loop in an async function.将 For 循环放入异步函数中。

like this:像这样:

const calls = async (urls2) => {
  for (let url of urls2.split(",")) {
    await axios.get(url, options)
    .then(response => {
      console.log(url);
      <--Rest of my code-->
    })
  }
}
calls(urls).catch(console.error);

I assume this last one failed because, while the function is async, everything inside of it is still synchronous.我认为最后一个失败了,因为虽然该函数是异步的,但它内部的所有内容仍然是同步的。

All I need is for each iteration of the loop to finish before the next one starts.我所需要的只是让循环的每次迭代在下一次开始之前完成。 What is the simplest way to do this?什么是最简单的方法来做到这一点?

const calls = async(urls2)=>{
   for (let url of urls2.split(",")) {
        const response = await axios.get(url,options); //will wait for response
        console.log(response.data);
        //rest of the code
   }


}

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

相关问题 在开始阅读下一个之前等待每个 playStream() 完成 - Wait for each playStream() to finish before starting to read the next one NodeJS 在继续之前等待 for 循环中的函数完成 - NodeJS Wait for function in for loop to finish before continuing 在执行下一个操作之前等待循环完成 - Wait for the loop to finish before performing the next action 如何等到 promise 完成后再开始 JavaScript 中的“无限”for 循环的下一次迭代 - How to wait until a promise is completed before starting the next iteration of an “infinite” for loop in JavaScript 在继续下一次迭代之前,NodeJS用管道完成了文件的写入 - NodeJS finish writing the file with pipe before continuing with the next iteration 如何在开始下一个功能之前等待功能完成? - How to wait for function finish before starting with next function? 在 nodejs 中执行下一次迭代之前等待 API 返回其响应 - Wait for API to return its response before executing the next iteration in nodejs 如何在NodeJs中发送回响应之前等待循环完成? - How to wait for a loop to finish running before sending back a response in NodeJs? 等待 mysql 在 for 循环中完成查询,然后在 nodeJS 中完成 function - Wait for mysql to finish queries in a for loop before finishing a function in nodeJS 等到 http 收到请求,然后再进行下一个循环迭代 - wait until http gets request before moving to next loop iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM