Reputation: 1209
I'm facing an issue regarding callback in Request js function. Given below is my code.
function getContent(address, result, callback){
request(address, function (err, response, body) {
if(err) throw err;
if (response.statusCode == 200) {
console.log(body);
}
});
callback(null, result);
}
Now when i run given code my callback is called and then my request function is hit. I want to execute my callback after execution of console.log(body) line. Kindly give me your suggestions about this issue.
Thanks in advance.
Upvotes: 0
Views: 174
Reputation: 1277
The callback() function should be inside the callback for the request. That ways, only when the request is completed would it get called.
function getContent(address, result, callback){
request(address, function (err, response, body) {
if(err) throw err;
if (response.statusCode == 200) {
console.log(body);
callback(null, result);
}
});
}
Upvotes: 1