0

I am newbie to nodejs.It's very hard to handle callbacks at nodejs level. I have code like this,

getItems(request,function(jsonObject){
     var itemData={};
     var itemDetails=new Array();
     for(var i=0;i < jsonObject.length;i++){
         getItemDetails(jsonObject[i].value.item_id,function(jsonObject){
            itemDetails.push(jsonObject);
         });
    }
    itemData["itemDetails"]=itemDetails;
    response.contentType("application/json");
    response.send({"data":itemData});
});

while executing the above code, the for loop is continuing with out getting callback from getItemDetails method and response sent to client. My requirement is the loop will wait until getting the call back from the getItemDetails then response should send. I have tried with process.nextTick(), but i am unable to find where i have to use that process.nextTick().. Please anybody provide suggestions.

Thanks in advance.

2 Answers 2

1

You need to send the response only after you get all the items, so modify your code like so:

getItems(request,function(jsonObject) {
    var itemData = {},
        itemDetails = [],
        itemsLeft = len = jsonObject.length,
        i;

    function sendResponse(itemDetails) {
        itemData["itemDetails"] = itemDetails;
        response.contentType("application/json");
        response.send({ "data": itemData });
    }

    for (i = 0; i < len; i++) {
        getItemDetails(jsonObject[i].value.item_id, function(jsonObject) {
            itemDetails.push(jsonObject);
            // send response after all callbacks have been executed
            if (!--itemsLeft) {
                sendResponse(itemDetails);
            }
        });
    }
});

Note: I've used itemLeft here since it's a more generic way to solve these kind of problems, but Ianzz approach is also ok since you can compare the length of the two arrays.

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

2 Comments

After changing the code also still i am facing the same problem. At least it is not checking the if condition, directly the i value changed and continuing further statements.
My bad, it was actually --itemsLeft, try now.
0

You can't get the loop to wait, but you can modify your code to get the behavior you are expecting:

getItems(request,function(outerJsonObject){
    var itemData={};
    var itemDetails=new Array();
    for(var i=0;i < outerJsonObject.length;i++){
        getItemDetails(jsonObject[i].value.item_id,function(innerJsonObject){
            itemDetails.push(innerJsonObject);
            if (itemDetails.length == outerJsonObject.length) {
                // got all item details
                itemData["itemDetails"]=itemDetails;
                response.contentType("application/json");
                response.send({"data":itemData});
            }
        });
    }
});

Comments

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.