How to exit from function which is called using wait.for.function()

27 views
Skip to first unread message

deepthi cherukuri

unread,
Sep 25, 2017, 12:34:04 PM9/25/17
to nodejs
Due to application requirements i have to use wait.for.function()

    var wait = require('wait-for-stuff'); 
    function a(){
    console.log("hello");
     return;
    }

    wait.for.function(a)
    console.log('end')

using above  way of calling the function..the wait.for.function() is waiting indefinetly..so any lines after wait.for.function(a) is not getting executed including the console statement after it.can some one tell me how to exit from the function so that the lines after wait.for.function() gets executed;

Mikkel Wilson

unread,
Sep 26, 2017, 2:33:45 AM9/26/17
to nodejs
Deepthi,

You've declared the function 'a' don't appear to be executing it. To execute the function, it needs to be called. You can do this in many ways, but either adding parentheses at the end of the function declaration like this:

    var wait = require('wait-for-stuff'); 
    function a(){
    console.log("hello");
     return;
    }(); // note () in bold here

    wait.for.function(a)
    console.log('end')

... or, like this:

    var wait = require('wait-for-stuff'); 
    function a(){
    console.log("hello");
     return;
    }

    a();
    wait.for.function(a)
    console.log('end')

Best,
Mikkel

Bruno Jouhier

unread,
Sep 26, 2017, 2:33:47 AM9/26/17
to nodejs
wait.for.function parameter must be a function that takes an async callback. Try changing function a to:

function a(cb) { 
  console.log(hello);
  process.nextTick(cb);
}

wait-for-stuff polls the event loop. This is a hack (see the deasync README?). Can you use async/await or fibers instead?

Bruno
Reply all
Reply to author
Forward
0 new messages