• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Async never resolves and keeps

Resolved: Async never resolves and keeps

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m having trouble finding out why my code never ends execution and gets stuck. It logs done before doing anything in the async run() block. The code just hangs with an output of:
Any ideas?

Answer:

According to the MongoDB NodeJS driver documentation, cursors (inheriting from AbstractCursor) have an [asyncIterator] symbol. An object that has that symbol implements a function that allows you to use them in a for await...of loop:
A for await...of loop will fetch one item from the cursor, run whatever is in its body, then repeat until the cursor is exhausted. You can use break, continue, try {} catch {} in the body, just like regular loops.
This will process all items after each other in series, which seems to be what you were aiming for.
If you want to run things in parallel, use a library that can take the AsyncIterator and turn it into batches, then process (for example) 5 at a time until it’s exhausted. But doing it in series is probably fast enough, it’s easier and less complex.

If you have better answer, please add a comment about this, thank you!

async-await javascript node.js
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Using AWK to count multiple patterns in a file

01/04/2023

Resolved: significance letter above bar graphic in wrong order

01/04/2023

Resolved: VBA – Applying border around the areas with value/text

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.