[英]setTimeout not working with node.js
我正在編寫mocha測試用例來測試以下步驟。 我打算在調用另一個API之前進行API調用並等待30分鍾。 我正在使用內部節點API編寫調用REST API來編寫此測試用例。 但由於某種原因, 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分鍾。 我放入的getPC
( getPC
方法)立即執行。
任何幫助表示贊賞。
謝謝
你的回叫是立即執行的,因為你正在那里召喚它。
將其更改為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
將啟動該計時器,而不是等待它完成。 一旦啟動該計時器,它將移動到下一位代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.