3
async.waterfall(
  [
  function(cb){
    //do some stuff and then never call cb leaving the async waterfall unfinished
  },...],
  function(err,result){
     //we only get here if the cb is called above and it never is
  }
)

Is there any harm in this? I realize it's not using the method as designed but I just found a bunch of code that I'm maintaining (written by someone who's no longer around to be yelled at) and I'm concerned that this might actually bog things down. If this was running on a server that was being hit heavily would it create a problem?

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

2

You need to callback final function otherwise it will not return to its calling code. Alternatively nest the callbacks inside each other or use promises. If you don't return the callback then the event loop will be blocked.

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
  • My question is, is there any harm in it not returning to its calling code? – Yevgeny Simkin Oct 29 '15 at 14:27
  • I have updated my answer, if you don't return callback the event loop is blocked until it is called back. – simon-p-r Oct 29 '15 at 15:03
  • thanks... I see http://elegantcode.com/2011/07/05/taking-baby-steps-with-node-js-dont-block-the-event-loop/ that blocking the event loop is bad, but are you sure that when this function completes its execution, prior to calling the callback, the event loop is actually blocked? Or put another way, if this is part of an Express instance, is anything else hampered if this particular event loop is blocked (indefinitely?) by this call? Is there a cap on simultaneous event loops? I'm trying to get my head around the bigger picture because I haven't seen this code producing any negative side effects – Yevgeny Simkin Oct 29 '15 at 17:51
  • I would suggest you write a test to test this specific function, if it is for example in an express request handler it may not return a http response to the client if it is blocked. – simon-p-r Oct 29 '15 at 18:54
  • It *is in an express request, but it does indeed return an http response *instead of calling the callback. Given such a model, does the blocked event loop actually hold anything up or bog anything down? – Yevgeny Simkin Oct 29 '15 at 23:57
  • Not if it is returning the http response, this is a signal that code is returning back to the event loop. Easiest way is to test concurrent requests to see if they both succeed. May need to rewrite the asnyc waterfall function – simon-p-r Oct 30 '15 at 00:02