Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:27:27+00:00 2026-06-18T06:27:27+00:00

Before you start shouting Do not use for-loops with async functions! let me state

  • 0

Before you start shouting “Do not use for-loops with async functions!” let me state that I do know how to program a loop with async functions calling the next iteration from within the callback.

But what, if I am not interested in the callback or in processing one iteration only if the predecessor has finished. In my case I am importing a big number of records from an xml-file and writing them into a mongoDB. The normal call to my “write”-function would be like this:

asyncWrite( record, function(err, result) {
    ...
});

Now as I said, im not interested in the callback (no errors will occur!) nor in a serial process. So I used a for-loop and left out the callback:

for( i=0; i<max; i+=1) {
    asyncWrite( record_arr[i] );
}

For small max this works perfectly. But with max getting bigger (> 100.000) I end up getting an ‘out of memory’ error. The only way to prevent this was turning back to a ‘normal’ async-loop (recursive function call from within the callback).

Any other idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:27:28+00:00Added an answer on June 18, 2026 at 6:27 am

    The async functions will not be executed until the for-loop has done its work. It has just collected a queue that is too beg. For big max you got out of memory error because the queue is full. You can see how your memory leaks by inserting console.log(process.memoryUsage()); inside of the loop. You need to work with chunks to process this task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before start let me tell my experience: I am experienced with C#.NET, web services,
I usually try to do TDD with not much analysis (no diagrams) before start
before I start I want to point out that I tagged this question as
Before I start, I want to make it clear that my code is working
Before i start, i know the MD5 is compromised (collision attack and speed of
Before start answering I know there is ReCaptcha which is simpler and easier, but
I get an error stating that I got an exception before start of a
Before I start writing huge swathes of code that don't work I thought I'd
Before you start firing at me, I'm NOT looking to do this, but someone
I want to get one record before start date and end date DtpFrom means

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.