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

  • SEARCH
  • Home
  • 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 8419463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:37:10+00:00 2026-06-10T02:37:10+00:00

I want to iterate an array asynchronously to unblock the execution. I’m using caolan/async

  • 0

I want to iterate an array asynchronously to unblock the execution. I’m using caolan/async to achieve that. When testing this code:

var ASync = require('async');

var arr = [];
for (var i = 0; i< 10; i++) {
    arr[i] = i;
}

var buf = "howdy";
ASync.forEach(arr, function(item, callback) {
    buf += item;
    callback();
}, function(err) {
    console.log(buf); // in the end
});
buf += "finished";

it shows this result:

howdy0123456789

I would assume it should show

howdyfinished0123456789

since I expect the Async lib to defer the execution. But why it isn’t?

  • 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-10T02:37:11+00:00Added an answer on June 10, 2026 at 2:37 am

    In your code, callback is executed on the same event loop iteration as foreach itself. As you never go outside of the current event loop iteration, buf += "finished" only executes after all buf += item were executed.

    The proper version is

    var ASync = require('async');
    
    var arr = [];
    for (var i = 0; i< 10; i++) {
        arr[i] = i;
    }
    
    var buf = "howdy";
    ASync.forEach(arr, function(item, callback) {
        process.nextTick(function () {
            buf += item;
            callback();
        });
    }, function(err) {
        console.log(buf); // in the end
    });
    buf += "finished";
    

    In the end, the buf will equal to howdy0finished123456789.

    However, this task is oversimplified. Why does one need the async loop at all?

    1. To perform async tasks in parallel. First, as each task is async, you won’t trigger the callback in the same event loop iteration anyway, rather giving it to the specific async task. Second, async.parallel is more suitable for this kind of task.

    2. To perform some CPU-intensive tasks, avoiding freezing the event loop (so that Node could process some tasks in between the iterations). This is what I’m assuming in my code example above. It would be quite hard to achieve this task using the plain JS (and its build-in Array.prototype.forEach), as you need to implement in some way the detection of the event all iterations of the loop has been processed.

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

Sidebar

Related Questions

I want to iterate over an array of inputs that belong to certain class
I have an array that I want to iterate over and delete some of
I add strings to an NSMutableArray and now I want to iterate that array:
I have these strings in an array and I want to iterate this array
I Have an array of different ids, I want to iterate this array and
I have a checkbox list creating an array that I want to iterate over
I have the array: example = ['foo', 'bar', 'quux'] I want to iterate over
I want to iterate through a reference to an array of hashes without having
I've got an array of words I want to iterate through on a page,
I have an json Object like this. {'01/19/2012': Array[1],'02/19/2012': Array[7],'03/19/2012': Array[6]} Now i want

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.