Update javascript parent variable from a callback


In node.js I need to collect some data across multiple callbacks and store them in one object. I tried the following code, but the collected data is not saved in the initialized variable. Can you pls help what I'm doing wrong?

My code:

for (var x in data) {
    var status = {items: []};

    for (var y in x) {
        do_something(y, function (err, res) {
            again_do_something(res, function(err, result) {
                status.items.push({key: result});

     console.log(status); //Here I got only {items: []}
Problem courtesy of: filo891



will be getting executed before the callback function getting executed. Thats why you are seeing empty.

One easy way to solve this common problem is to use async library's concat method.

Solution courtesy of: thefourtheye


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.