6

I need some help with the asynchronous nature of node.js. I have a for loop, which collects data from the database. "result" is an array, which should then be returned to the main function.

user_collection.findOne({
            _id : uid
        }, function(error, user) {
            if(error)
                callback(error)
            else {
                for(var j = 0; j < user.contacts.length; j++) {
                    if(user.contacts[j].accepted == 'true') {
                        user_collection.findOne({
                            _id : user.contacts[j].contactId
                        }, function(error, user) {
                            result.push(user);
                        })
                    } 
                }
                callback(null, result);  // This callback executes before the for-loop ends, ofc
            }
        });

How can I ensure that the callback executes after the loop finished?

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
johnny
  • 8,696
  • 6
  • 25
  • 36
  • Working version with async: `user_collection.findOne({ _id : userId }, function(error, user) { if(error) callback(error) else { async.forEach(user.contacts, function(contact,callback) { console.log(contact); if(contact.accepted == 'true') { user_collection.findOne({ _id : contact.contactId }, function(error, contact) { result.push(contact); callback(); }) } }, function(error) { callback(error, result) }) } });` – johnny May 04 '12 at 16:03

3 Answers3

11

You might want to consider using helper library like async https://github.com/caolan/async

It helps keep code more consistent..

In your case, you can look at the forEach() method

forEach(arr, iterator, callback)

The iterator is called with an item from the list and a callback for when it has finished.

Checkout the unit tests for examples

https://github.com/caolan/async/blob/master/mocha_test/each.js

Victor Palade
  • 190
  • 2
  • 12
250R
  • 35,945
  • 7
  • 33
  • 25
2

Using ES6 Promises (a promise library can be used for older browsers):

Process all requests guaranteeing synchronous execution (e.g. 1 then 2 then 3)

function asyncFunction (item, cb) {
  setTimeout(() => {
    console.log('done with', item);
    cb();
  }, 100);
}

let requests = [1, 2, 3].reduce((promiseChain, item) => {
    return promiseChain.then(new Promise((resolve) => {
      asyncFunction(item, resolve);
    }));
}, Promise.resolve());

requests.then(() => console.log('done'))

Process all async requests without "synchronous" execution (2 may finish faster than 1)

let requests = [1,2,3].map((item) => {
    return new Promise((resolve) => {
      asyncFunction(item, resolve);
    });
})

Promise.all(requests).then(() => console.log('done'));

I did it on that way

Promise.all(body.schedules.map(function(scheduleId) {
        return new Promise(function(resolve, reject) {
            return scheduleSchema.findOneAndRemove({
                    _id: scheduleId
                })
                .then(function() {
                    logSchema.insert({
                        userId: req.user.id,
                        actId: constants.LOG_SCHEDULE_DELETE.id,
                        extData: scheduleId
                    });
                    resolve();
                })
                .catch(function(err) {
                    reject(err);
                });
        });
    })).then(function() {
        return res.json({
            code: constants.SUCCESS_CODE
        });
    }).catch(function(err) {
        return res.json(constants.DATABASE_ERROR);
    });

The last example

function callback (result) { console.log('all done'); }

[1, 2, 3].forEach((item, index, array) => {
  asyncFunction(item, () => {
    if (index === array.length - 1) {
      callback();
    }
  });
});

This does not guarantee that callback will execute after all items are processed. It only guarantees that callback will execute after the very last item is processed.

More information

Michael.

Michael Horojanski
  • 4,543
  • 4
  • 33
  • 34
0

With v1.5.2 of Async.js, It is each.

each(arr, iterator, [callback])

arr - An array to iterate over.
iterator(item, callback) - A function to apply to each item in arr.
callback(err) - Optional. A callback which is called when all iterator functions have finished, or an error occurs.

Sridhar
  • 11,466
  • 5
  • 39
  • 43