Help with asynchronous method

96 views
Skip to first unread message

Duy Nguyen

unread,
Feb 21, 2016, 10:35:01 PM2/21/16
to nod...@googlegroups.com
Hi,

I want to my code work as below:
1. save Chest object to db
2. Then save all contents belong to this chest to db (have logic to add new filed to content before saving)
3. Then return response with chest + new contents info

But the real code does not work as I expected, the code is:

chestApi = createChestWithContents{
    var chest = req.body;
    var contents = req.body.contents;
    chest.save(function(err, chest) {
           async.each(contents, function(content) {
                     content.save()....
            }, function(done) {
                   return res.status.(200).. ///OK
            });
    });
}

When I use postman to test, the method end up without any response returned due to asynchronous methods(even when I used async to wait till the loop finish), cannot figure out how to do those stuff synchronously.
--
Nguyen Hai Duy
Mobile : 0914 72 1900
Skype: nguyenhd2107

Emanuele DelBono

unread,
Feb 22, 2016, 12:01:38 PM2/22/16
to nod...@googlegroups.com
Probably you are using async.js in a wrong way. Do you call the "done callback" on after every save of the chest? The callback for async.each received 2 parameters, the item and the done callback.


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAGOR8-2A8LGnQugzH92quVwEDCiGOBExp7_mq%3DAtjp-LaDt4_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--

Christopher Mina

unread,
Feb 23, 2016, 9:12:26 AM2/23/16
to nodejs
To be clear about Emanuele's response, the async API should look like this.

async.each(contents, function(content, cb) {
     content.save(function(err, resp) {
         cb(err); //lets hope err is null, but if it's not null, the err will get propagated through to the final "errors" param below.
     });
}, function(errors) {
    res.status.(200).. ///OK
});


-Chris

Duy Nguyen

unread,
Feb 23, 2016, 10:12:47 AM2/23/16
to nod...@googlegroups.com
Thanks Christopher and Emanuele.

I do not want to call cb after saving, mean that I just want to loop via list of contents, then save them, thats all.

async.each(contents, function(content, cb) {
     content.save(function(err, resp) {
         /*DO NOT NEE TO CALL cb HERE*/ cb(err); //lets hope err is null, but if it's not null, the err will get propagated through to the final "errors" param below.
     });
}, function(errors) {
    res.status.(200).. ///OK
});

Is it possible to apply async without cb param?




For more options, visit https://groups.google.com/d/optout.



--

Christopher Mina

unread,
Feb 23, 2016, 2:16:51 PM2/23/16
to nod...@googlegroups.com
You would simply just call save and then cb(). 

async.each(contents, function(content, cb) {
     content.save(
);
     cb();
}, function(errors) {
    res.status.(200).. ///OK
});


You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/SdUS7BE6eRQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Christopher Mina

Live Healthy. Work Hard. Be Kind. Gain Success.

Duy Nguyen

unread,
Feb 24, 2016, 6:18:46 PM2/24/16
to nod...@googlegroups.com
It works, thank you so much Chris.


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages