Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout not working with node.js

I am writing mocha test cases to test the following steps. I intend to make an API call and wait for 30 minutes before calling another API. I am using an internal node API which was written to call REST APIs to write this test case. But for some reason, setTimeout is not waiting for the given ms. Can someone please help me?

 describe('Checkout - ', function() {
   before(function() {
     lapetus = test.Lapetus;
   });
  it('Get purchase contract after session is expired [C123]',     function(done) {
    this.timeout(180000000);
    lapetus.run(function() {
     // create customer
     ......

     // create new cart and add one item
     ......

    // create new contract with empty cart id
    .......
    var pc_id =....;

    // wait for 30 minutes for the session to expire
    console.log('wait... ' + new Date);
    this.setTimeout(getPC(lapetus,pc_id), 18000000);
    console.log('ok... ' + new Date);
    done();
  });
});

  var getPC = function(lapetus, pc_id){
   // get newly created purchase contract and verify session expired message throws
    .....
    ......
 };
  });

It does not wait 30 minutes. The call back I put in (the getPC method) executes immediately.

Any help is appreciated.

Thanks

like image 440
user3773712 Avatar asked Apr 23 '15 20:04

user3773712


People also ask

Does setTimeout work in node JS?

The setTimeout function is used to call a function after the specified number of milliseconds. The delay of the called function begins after the remaining statements in the script have finished executing. The setTimeout function is found in the Timers module of Node. js.

Why is setTimeout not working?

If you are requiring the setTimeout functions to execute on the dot, there can be some scenarios when this is not the case. Late timeouts or timeouts that execute inaccurately could be due the following issues: browser or OS is busy with other tasks.

Does setTimeout block NodeJS?

Explanation: setTimeout() is non-blocking which means it will run when the statements outside of it have executed and then after one second it will execute.

What can I use instead of setTimeout?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.


1 Answers

Your call back is executing immediately because you're calling it then and there.

Change it to this.setTimeout(function() { getPC(lapetus,pc_id); }, 18000000); so that what you want to execute is in a function for setTimeout to call.

** Edit **

In relation to my last comment. You should move your "ok..." inside of the function you've put inside of setTimeout. This will cause the "ok..." to execute right before getPC is called.

this.setTimeout(function() {
    console.log('ok... ' + new Date);
    getPC(lapetus,pc_id)
}, 18000000);

It is important to understand that setTimeout will only start a timer which will execute your code later. setTimeout is going to start that timer, and not wait for it to finish. It will move to the next bit of code once it is done starting that timer.

like image 94
Chris Avatar answered Oct 24 '22 01:10

Chris