0

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.

1 Answer 1

1

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); 
    }
});
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.