help icon indicating copy to clipboard operation
help copied to clipboard

Async/Await not working inside for loop for loop me in any case

Open amanullah68 opened this issue 5 years ago • 5 comments

v8.16.0 Ubuntu 18.04 LTS

Hi guys, I'm trying to fetch values from other server through my node.js code. I'm using for loop for this purpose and wanted to wait inside for loop to get value first then move to 2nd iteration and so on. For this I'm putting it in aync/await but failed as for loop not wait for the server result then iterate next. Is async/await works inside for loop????

amanullah68 avatar Sep 04 '20 04:09 amanullah68

Could you provide a minimal code that reproduces the issue?

vsemozhetbyt avatar Sep 04 '20 06:09 vsemozhetbyt

async/await works inside for-loops. the problem probably lies in the code. If you ask a question it's recommended to post a code snippet with it so we can be helpful

Lancear avatar Sep 08 '20 20:09 Lancear

I confirm. I have await function that do a call to Spotify API inside for of loop and it not wait. It's diffictult to provide minimal example. Node version 14.15.4

Behinder avatar Feb 04 '21 13:02 Behinder

Works perfectly fine for Node v14.15.4 LTS

const asyncTimeout =(timeout)=> new Promise(res => setTimeout(res, timeout));

main();
async function main() {
  for (let counter = 0; counter < 10; counter++) {
    await asyncTimeout(1000);
    console.log(`${counter}: ${new Date().toISOString()}`);
  }
}

So there must be an error in the code with which we can only help if you provide a minimal example.

Lancear avatar Feb 04 '21 17:02 Lancear

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 11 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 10 '24 01:06 github-actions[bot]

This bug still exists as far as I can see...

alfasin avatar Jan 22 '25 11:01 alfasin