My code was working until i updated node.js to version 8.11.3
Now i always get error "callback argument must be a function" when trying to call a function with setTimeout.
function testFunction(itemid, price) {
var url = 'https://example.com';
var options = {
method: 'get',
url: url
}
request(options, function (err, res, body) {
var response = JSON.parse(body);
if(response.status == 'fail'){
setTimeout(testFunction(itemid, price), 100);
}
})
}
Callback argument for setTimeout
must be a function. Write it like this. Not tested but it should work.
function testFunction(itemid, price) {
var url = 'https://example.com';
var options = {
method: 'get',
url: url
}
request(options, function (err, res, body) {
var response = JSON.parse(body);
if(response.status == 'fail'){
setTimeout(function () {
testFunction(itemid, price);
}, 100);
}
})
}
@keyur is correct. According to the node.js timers documentation the setTimeout function takes the name of the function to execute as the first argument, the delay in milliseconds as the second argument, followed by any arguments being passed to the function.
For Example:
setTimeout(testFunction, 100, itemid, price);
Yes, setTimeout() expects first argument as callback function. we can make use of es6 fat arrow function here you can try this!
setTimeout(() => testFunction(itemid, price), 100);
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With