4

I want to make non-blocking loop over array with objects, so I used async.each function:

log.info("before");

async.each(products , function(prdt, callback){
    for(var i = 0 ; i <4000000000; i++){
        var s = i;
    }
    console.log("inside");
    callback();

}, function (err) {
    console.log("end");
});
log.info("after");

So if I run code above I have a such messages output:

before
inside
....
inside
end
after

If async.each asynchoronous why I dont see output in that order?

before 
after
inside 
inside..
end 

UPDATE1: thx for answers, but If I want to execute that code inside my router, I will blocked all responces to my server? What I need to change?

12
  • github.com/caolan/async/blob/… Commented Jul 21, 2015 at 22:50
  • "What I need to change?" --- a programming language. JS (its userland, to be precise) runs in a single thread. Commented Jul 21, 2015 at 22:55
  • so, how I can itearate over array asynchronously and dont block user responces to my server? Commented Jul 21, 2015 at 22:56
  • You cannot. JS is single threaded. So whenever you run this CPU-bound code everything else will wait. Commented Jul 21, 2015 at 22:57
  • So no matter what I use simple 'products.forEach' or async.each? When I iterate over array I block my whole server? Commented Jul 21, 2015 at 23:00

2 Answers 2

2

Seems to me that the async.each function is simply implying that it can be used for asynchronous actions because it inherently includes a callback function (which is trivial for you to add yourself in your own function).

Consider this code which uses Mongoose (a MongoDB wrapper) to simulate a real-world asynchronous call:

console.log("before");

async.each(["red", "green", "blue"], function(color, cb) {

    mongoose.model('products')
        .find({color: color})
        .exec(function(err, products) {
            if (err) return cb(err); // will call our cb() function with an error.
            console.log("inside");
            cb(null, products);
        });

}, function(err, products) {
    if (err) return console.error(err);
    console.log("really after");
    console.log(products);
});

console.log("after");

You'll get

before
after
inside
really after
[red products]
inside
really after
[green products]
inside
really after
[blue products]

Does it make sense why? Let me know if I can break any of this down further.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, you give me what I want to see.
0

async.each() is asynchronous, but you're not doing anything that is blocking or requires an asynchronous loop. If you put a setTimeout() in there you'd see it working like you expect.

3 Comments

"async.each() is asynchronous" --- what does this exactly mean? I provided a link to the source which states that it runs a callback synchronously
"If you put a setTimeout() in there you'd see it working like you expect." --- what is the point in using async.each if you still need setTimeout()?
setTimeout() is simply an example of code that triggers an asynchronous event. That is, a setTimeout() inside a function inherently means some code will run in a future event loop, even through the function that wraps the setTimeout() call will return immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.