Wednesday, 15 March 2017

How to wait for the last callback in a series of callbacks?

I am using node's express to expose a computation library via REST api.

app.get('/', function(req, res) {
  performComputation(data,function(result){
     res.send(result);
  };
}

I cannot change the interface or any internal of the lib being used. The problem is, that the library calls the result callback multiple times. For each call a new computation result is added to result. The first time the callback is called it may contain [1], second [1,91], third [1,91,31] and so on.

I don't know in advance how many times the callback will be called. What needs to be send via req.send() is only the result submitted within the last callback performed. This result does already contain all the values that were published in the previous callbacks.

So how to "wait" for the last callback or achieve this in another way?



via k_wave

No comments:

Post a Comment