0

I am unable to get the value of the response outside the callback code. It returns undefined outside whereas in the callback it is giving proper result.

function doCall(urlString, callback) {
    request.get(
        urlString,
        null,
        null,
        (err, data, result) => {                              
            var statusCode = result.statusCode;
            return callback(data);
        }
    );
}

const apiResponse = doCall(urlString, function(response) {
    console.log('***************************' + response); //Prints correct result
    return JSON.parse(response);
});

console.log('+++++++++++++++++++++++++' + apiResponse); //Prints undefined
5
  • You will need to use a promise or async/await Commented Jul 23, 2019 at 19:47
  • Your function doCall does not return anything, so affecting its return "value" to apiResponse sets its value to undefined. Commented Jul 23, 2019 at 19:50
  • but doCall is returning 'return callback(data)'. but is that not correct? Commented Jul 23, 2019 at 20:02
  • @Jeff how will using async help me get a value outside of the function. Commented Jul 23, 2019 at 20:03
  • You need to read up on how the event loop and async works in node. You need to setup something like var response = await getResponse() from a promise. Commented Jul 23, 2019 at 20:07

2 Answers 2

1

function doCall(urlString) {
    return new Promise((resolve, reject) => {
        request.get(
            urlString,
            null,
            null,
            (err, data, result) => {
                if (error) reject(error);
                var statusCode = result.statusCode;
                resolve(data);
            });
    });
}

async function myBackEndLogic() {
    try {
        const result = await doCall(urlString);
        console.log(result);
       //return JSON.parse(result) if you want

    } catch (error) {
        console.error('ERROR:');
        console.error(error);
    }
}

myBackEndLogic();

Read this for more explanations

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for sharing this. I was able to run the code.
Welcome. Read the link i provided. It will help to clear lots of confusions in nodejs.
I would also highly recommend this course on Udemy (udemy.com/the-complete-nodejs-developer-course-2). Even if you have a pretty good background in programming it covers a lot of things that are unique/different with nodejs.
Thank you Jese and Jeff. The links are very useful. I am very grateful. Thank you!
0

If you want synchronous looking code, wrap everything in an async function:

(async (){
    async function doCall(urlString, callback) {
        return await request.get(urlString, null, null); // or store in a variable and return modified response
    }
    const apiResponse= await doCall(urlString, (response) => {
        console.log('response', response);
        return JSON.parse(response);
    });
    console.log('apiResponse', apiResponse);
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.