SetTimeout does not work with node.js

I am writing mocha test cases to test the following steps. I intend to make an API call and wait 30 minutes before calling another API. I am using the internal node API that was written to call the REST API to write this test case. But for some reason setTimeoutdoes not wait 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
    .....
    ......
 };
  });

He does not wait 30 minutes. The callback that I insert (the method getPC) is executed immediately.

Any help is appreciated.

thank

+4
source share
1 answer

Your callback is executed immediately because you call it then and there.

this.setTimeout(function() { getPC(lapetus,pc_id); }, 18000000); , , , setTimeout.

** **

. "ok..." , setTimeout. , "ok..." getPC.

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

, setTimeout , . setTimeout . , .

+9

All Articles