使用回调在 setTimeout 中递归调用 nodejs 函数



我正在尝试使用回调在setTimeout中递归调用nodejs函数。附加代码片段。它没有按预期工作。我错过了什么吗?

Model.xyz= function(cb){
//do something here and get the result.
if(result<10)
{
setTimeout(function(){ 
Model.xyz(cb);
},5000);
}
//once result is > 10 execute following code
}

只需使用将永远调用 Your 方法

const async = require('async');
Model.xyz = cb => {
// do somethings
if(result < 10) return cb(null, true);
cb();
}
async.forever(cb => {
Model.xyz(repeat => {
// finish call and schedule next call
if(repeat === true) return setTimeout(cb, 5000);
cb('exit');
});
});

最新更新