1

I am trying to implement parallel(funcList, done) to implement the call to each of the functions passed at the same time.

    parallel([
    function(cb) { setTimeout(1000, cb); },
    function(cb) { setTimeout(2000, cb); },
    function(cb) { $.get('example.com').always(cb); }
],
function() { alert('done'); }

);

Any pointers would be a great help..

adelphia
  • 187
  • 1
  • 2
  • 14
  • so is `cb` the final callBack? or is that supposed to be some other function? What exactly do you want to happen? Might need some more info. – James Aug 06 '15 at 00:13
  • 1
    You can't really reliably have true simultaneous execution, at least not in browser environments. Concurrency is event-driven in the JavaScript runtime. The closest approximation you could have here is to have all your function inputs in `parallel()` executed with `setTimeout(f, 0)` – your functions won't truly run simultaneously, but they will at least run independently and the `parallel()` function will return immediately to the caller. What is it you're really trying to achieve here? – David Pisoni Aug 06 '15 at 00:14
  • I have this which probably is close to what you are suggesting David: `var parallel = function(fcnList, done) { var i = 0; for (i = fcnList.length(); i>0; i--) { (function(arg) { setTimeout(function() { var fun = fcnList.pop(); fun(); }, 0); }( (i); done(); };` – adelphia Aug 06 '15 at 00:51
  • James, assuming cb is a callback function for each of the function passed in, – adelphia Aug 06 '15 at 00:56
  • @DavidPisoni: You perhaps forgot about web workers? – slebetman Aug 06 '15 at 02:22
  • @slebetman sure, but it wasn't clear that was what the questioner wanted. – David Pisoni Aug 06 '15 at 19:09
  • @DavidPisoni: I was just commenting on your statement: "You can't ... have true simultaneous execution ... in browser". – slebetman Aug 07 '15 at 03:09
  • @slebetman: yeah, that was just wrong. :) – David Pisoni Aug 07 '15 at 17:06

1 Answers1

1

The async library has a method that does what you are looking to do and is very reliable. https://github.com/caolan/async#parallel

  • Thanks! I was looking at doing this without using any framework. – adelphia Aug 06 '15 at 01:09
  • 1
    @annukath: It's not a framework. It's just a library. And each function in that library is mostly independent of each other so you can probably just copy the `parallel` function and use it directly in your project (note that you must respect the open-source license). If you're interested in how the functions in the async library work you may want to look at my answer to this old question (posted before the async library was written): http://stackoverflow.com/questions/4631774/coordinating-parallel-execution-in-node-js/4631909#4631909 – slebetman Aug 06 '15 at 02:27
  • Note that if you decide to use my stackoverflow answer instead of the async library, you should respect the stackoverflow license (in some ways simpler than the license async uses): http://stackoverflow.com/help/licensing – slebetman Aug 06 '15 at 02:29
  • annukath, you're correct, I used the wrong word. Updated the answer, it is a library not a framework. – datfinesoul Aug 07 '15 at 13:12