0

I have task to search meaning of certain words via lookup on a database for which i am making asynchronous calls to the database, every request would look for sat n number of terms. The issue i have is I want to call another call back, say, grand_callback, the goal of this callback is to aggregate data from all other callbacks and process the next set of codes after aggregating all the data.

Is there a way I can implement the same..

Some details:

terms = [........] // 1000 terms
grand_callback = () ->
  #called with aggreagted data.
getbucket_data = (bucket ,callback) ->
  #some treatment over terms 
  callback null , data
some_func = (term) ->
  bucket.push term
  if bucket.length is 15{
     getbucket_data bucket , (err, data)->
        #i need to aggregate this data
}
_.map terms , some_func 
user229044
  • 232,980
  • 40
  • 330
  • 338
Ankit Solanki
  • 670
  • 2
  • 9
  • 23
  • You can either uses promises or your own counter. In either case, some piece of code keeps track of when all the async calls to the database are done (generally by counting when there are no more outstanding requests) and then that code calls the grand_callback with the accumulated data. Folks can help you much more specifically if you provide your actual code structure. – jfriend00 Apr 18 '14 at 23:45
  • Here are some examples [here](http://stackoverflow.com/questions/21342124/detect-when-ajax-is-done/21342295#21342295) and [here](http://stackoverflow.com/questions/22164037/using-jquery-deferred-or-promise-to-wait-for-multiple-post-calls-to-finish/22164126#22164126) and [here](http://stackoverflow.com/questions/19914721/javascript-callback-waiting-for-multiple-functions-to-end/19914849#19914849). – jfriend00 Apr 18 '14 at 23:48
  • Thanks all for your suggestions. – Ankit Solanki Apr 19 '14 at 00:16

1 Answers1

0

You can, of course, just keep track of which callbacks are done manually, but this can be a pain and pretty error prone if you have to do it a lot. Perhaps you could benefit from one of these solutions:

1. Use an async library

I personally like using async by caolan. It has a bunch of functions that can be useful for managing asynchronous operations; the one you're looking for is probably parallel (docs):

parallel(tasks, [callback])

Run the tasks array of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error. Once the tasks have completed, the results are passed to the final callback as an array.

So you would do something like:

async.parallel([
  asyncFunctionOne,
  asyncFunctionTwo,
  asyncFunctionThree
], function(error, results) {
  // ...
});

2. Use promises

Promises are a nice abstraction on top of asynchronous operations. A promise represents a value that either exists now or will exist in the future; you don't have to care which. You can wait for multiple promises to complete by creating a new promise that contains them all. One of the most popular promise libraries for Node is Q by kriskowal.

// creating a promise manually
var deferred1 = Q.defer();
var promise1 = deferred1.promise;

asyncFunction1(function(err, value) {
  if (err) deferred1.reject(err);
  else deferred1.resolve(value);
});

// wrapping a Node-style function to create promises
var promise2 = Q.nfcall(asyncFunction2, arg1, arg2);

// Create a promise that waits for all other promises
var grandPromise = Q.all([promise1, promise2]);
grandPromise.then(Q.spread(function(promise1result, promise2result) {
  // ...
}));
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311