How to use wait.for?

163 views Asked by At
var blah = require('wait.for');
var wait = require('wait.for/parallel-tests');

function  testFunction(){
  console.log('fiber start');
  wait.miliseconds(10*1000);
  console.log('fiber end');
};

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

When I try to run the above code I get the following output

app start
fiber start
Z:\nodeplayground\node_modules\wait.for\waitfor.js:15
Fiber( function(){ fn.apply(null, newargs)} ).run(); //launch new fiber,
call the fn with the args, this=null (strict)
^
TypeError: wait.miliseconds is not a function
at testFunction (Z:\nodeplayground\index.js:8:10)
at Z:\nodeplayground\node_modules\wait.for\waitfor.js:15:31

What am I doing wrong?.

The main functionality that I want to achieve is the following

function testFunction2(){
   function Innerfunction(){
      setTimeout(function(){
         console.log("hello");
      },5000);
      console.log("After setTimeout")
  }
}

I want the above code to wait for 5 seconds and print "hello" and after that "After setTimeout".

1

There are 1 answers

0
Freez On

You try to import milliseconds method which is not exported by the module wait.for/parallel-tests.

You have to declare wait.milliseconds function in your main file (or wrap it in a module for reusability).

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

wait.timeout_callback = function(ms,callback){
  setTimeout(callback,ms); //call callback(null,null) in ms miliseconds
}

wait.miliseconds = function(ms){
  wait.for(wait.timeout_callback,ms);
}    

function testFunction(){
  console.log('fiber start');
  wait.miliseconds(10*1000);
  console.log('fiber end');
};

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