0

I am trying to use async.waterfall to create a response in my express services. Here is my code:

var getUserObject = function(id, res) {
  'use strict';
  async.waterfall([
    function(callback) {
      getCompleteUserObject(callback, id);
    }
  ], function(err, result) {
    res.json(result.user);
  });
  // Function returns here
};

I want my return statement to be the res.json(result.user), but the waterfall function always returns where I have the commented code above. I use series and parallel function all the time and they work find. What am I doing wrong?

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
jhamm
  • 24,124
  • 39
  • 105
  • 179
  • 3
    What do you mean that you want the return statement to be the `res.json(result.user)`? The nature of the `waterfall` call is that it's async, so, it doesn't have a result and returns immediately. `getUserObject` can't return a value that was computed in the call to `waterfall`. – WiredPrairie Jan 12 '14 at 14:57
  • What you're trying to do is an exact duplicate of [this infamous question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call), only in node. You can't return the result of an async method into a variable to be used later, you have to wait for the async call to finish, and that's what callbacks are for, the async middleware is just a helper to avoid "callback hell". – adeneo Jan 12 '14 at 15:52

0 Answers0