luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

wait.parallel.launch - does not pass in callback #25

Closed codeuniquely closed 9 years ago

codeuniquely commented 9 years ago

it may just be that I'm not calling it right but

var wait=require('wait.for');

function func1(req, done) {
    console.log(req);
    console.log(done);
    done(null, 'write some code for 1');
}
function func2(req, done) {
    console.log(req);
    console.log(done);
    done(null, 'write some code for 2');
}
function test() { 
    var arrFunc = [];

    arrFunc.push([func1, "hi 1"]); 
    arrFunc.push([func2, "hi 2"]);
    var data = wait.parallel.launch(arrFunc);
    console.log(data);
}

wait.launchFiber(test);

I was expecting to get some output in data and then log it but what I actually get is

'hi 1'
undefined
TypeError: undefined is not a function
    at func1 (e:\project\test\index.js:6:9)
    at wait.parallel.fiberForItemBody (e:\project\test\node_modules\wait.for\waitfor.js:162:39)
    at e:\project\test\node_modules\wait.for\waitfor.js:15:31

It seems that the callback ('done' in this case) is not being passed in and the callback fails. The same happens with any request that I could normally pass into a wait.for() call.

wait.for(func1, 'normal wait call');
luciotato commented 9 years ago

wait.parallel.launch expects an array of [[fn,arg,arg..],[fn,arg,arg..],...] and then launches a fiber for each function call, in parallel and waits for all the fibers to complete. The functions should be sync/normal functions and the calls are made without adding any parameters

in wait.parallel.launch(arr) The functions to be called _should not be async functions_. Each called function will be executed in a fiber, and this sync called function should/can use wait.for internally in order to call async functions.

It seems that you're assuming the functions to be called should be async and that's not the case for wait.parallel.launch. it's this way because the same parallel.launch is used to implement parallel.map and parallel.test, and for those cases, a normal non-async mapping/test function is the most common use-case

Here's fixed:

var wait=require('./waitfor');

function fdone(p1,p2){
    console.log(p1,p2);
}

function func1(req, done) {
    console.log(req);
    console.log(done);
    done(null, 'write some code for 1');
}
function func2(req, done) {
    console.log(req);
    console.log(done);
    done(null, 'write some code for 2');
}
function test() { 
    var arrFunc = [];

    arrFunc.push([func1, "hi 1", fdone]); // all func1 required parameters 
    arrFunc.push([func2, "hi 2", fdone]); // all func2 required parameters
    var data = wait.parallel.launch(arrFunc);
    console.log(data);
}

wait.launchFiber(test);
codeuniquely commented 9 years ago

Just a suggestion but maybe it would be a good idea to put that in the documentation as an example on how to use those functions

luciotato commented 9 years ago

It's a good idea. I've already added more detail in the README for wait.parallel.launch Maybe you have a better example of a real-use case of wait.parallel.launch? In a real use case, the called functions should use wait.for internally (no real "parallelism" if they not) and it should not be a case best covered by wait.parallel.map|filter