node.js - How to return value of db calls from a asynchronous function -
i looking answer , found this. in jquery there when
function. using guidelines synchronize asynchronous calls using async.js
in node here code.
async.foreach(dateindateformat, function(item, callback) { console.log(moment(item.startdate).todate()); var result = trip.aggregate([{ "$unwind": "$trips" }, { "$match": { "trips.starttime": { "$gte": moment(item.startdate).todate(), "$lte": moment(item.enddate).todate() } } }, { "$group": { "_id": { "date": { "$dayofmonth": "$trips.starttime" } }, "distance": { "$sum": "$trips.distance" } } }]); result.exec(function(err, doc) { console.log(doc); }); console.log("executing callback"); callback(); }, function(error) { console.log("loop over"); });
this output.
executing callback loop on [ { _id: { date: 23 }, distance: 0 }, { _id: { date: 22 }, distance: 0 }, { _id: { date: 21 }, distance: 0 } ] [ { _id: { date: 29 }, distance: 210 }, { _id: { date: 27 }, distance: 210 }, { _id: { date: 26 }, distance: 210 }, { _id: { date: 25 }, distance: 0 } ] []
as can see. executing callback , loop on executed before data. cannot send angular frontend using res.send()
. 1 answer cannot return value asynchronous function. return callback. how do in case? or how use async.series([])
function. because used , not giving me exact results. thanks
edit answer in short, position of callback function matters. if give @ wrong place don't expect write answers. please see code in answer , comment thread better explanation.
the callback
kind of magic , indicates async.foreach
specific foreach
execution has completed. not familiar aggregate
function or exec
, i'm assuming aggregate builds query, , exec
executing query asynchronously.
the foreach callback moved result.exec
result. indicate async
execution of particular query over. when executions on function passed second param foreach
executed. when executed, functions have completed or error has occurred, in case can send
res
.
the overall flow:
- create list of data operate on asyncronously
- execute function (2nd param async.foreach) against each item in list asyncronously.
- build aggregation query
- exeucte aggregation query asynchronously
- indicated
async.foreach
aggregate operation has been completed callingcallback
function
- when
callback
has been called each item in array async call function passed 3rd paramasync.foreach
. - return result client because tasks have been resolved.
async.foreach(dateindateformat, function(item, callback) { console.log(moment(item.startdate).todate()); var result = trip.aggregate([{ "$unwind": "$trips" }, { "$match": { "trips.starttime": { "$gte": moment(item.startdate).todate(), "$lte": moment(item.enddate).todate() } } }, { "$group": { "_id": { "date": { "$dayofmonth": "$trips.starttime" } }, "distance": { "$sum": "$trips.distance" } } }]); result.exec(function(err, doc) { console.log(doc); console.log("executing callback"); callback(); }); }, function(error) { // tasks passed foreach have been completed, or // error has occurred res.send('complete'); console.log("loop over"); });
it's crazy me how simple operation can have such crazy programming flow :)
Comments
Post a Comment