luciotato / waitfor

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

Can't get wait.for to wait #13

Closed iamerikjolson closed 10 years ago

iamerikjolson commented 10 years ago

I've read and re-read your github documentation, and still can't get node to wait. What am I doing wrong?

var wait = require('wait.for'), testFunction = function(){ console.log('fn start'); wait.for(setTimeout, function(){}, 5000); console.log('fn end');
};

console.log('app start'); wait.launchFiber(testFunction); console.log('app end');

The expected outcome is:

Instead what I get is

luciotato commented 10 years ago

wait for requires a standard async function, with callback(err,data) as last parameter. setTimeout do not callback. you do not get 'fn end' because the function at setTimeout never call back.

try this (not tested)

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

function standardAsync(param, callback){
    setTimeout( function(){
                  console.log(param);
                  callback(null,param);
        }, 5000);
};

function  testFunction(){
    console.log('fn start');
    wait.for(standardAsync,'test');
    console.log('fn end');
};

console.log('app start');
wait.launchFiber(testFunction);
console.log('after launch');