setTimeout不能和node.js一起工作

我正在写摩卡testing用例来testing以下步骤。 我打算做一个API调用,并等待30分钟,然后再调用另一个API。 我正在使用一个内部节点API编写来调用REST API来编写这个testing用例。 但由于某些原因, setTimeout并不等待给定的ms。 有人可以帮帮我吗?

  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 ..... ...... }; }); 

它不会等待30分钟。 我放入的getPCgetPC方法)立即执行。

任何帮助表示赞赏。

谢谢

您的回拨正在立即执行,因为您在那里和那里调用它。

将其更改为this.setTimeout(function() { getPC(lapetus,pc_id); }, 18000000); 所以你想要执行的是一个setTimeout函数来调用。

** 编辑 **

关于我最后的评论。 你应该在setTimeout放置的函数中移动“ok …”。 这会在getPC之前执行“ok …”。

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

重要的是要明白, setTimeout将只启动一个计时器,稍后将执行您的代码。 setTimeout将启动该计时器,而不是等待它完成。 一旦启动该定时器,它将移动到下一个代码位置。