0

What I am trying to do is to make a request to a Web API and then if the input is valid return true from a function and if it is not valid return false. I want the request to be async but I don't want the function to return before the request is validated. While the request is being made, I just want to continue to process input.

Here is the important part of the function:

request({
        url: url,
        json: true
    }, function (error, response, body) {

        if (!error && response.statusCode === 200) {
            console.log(JSON.stringify(body));
            if(body.player !== "null") {
                console.log(body.player.socialMedia.links["DISCORD"]);
                console.log((client.users.get(author).username + "#" + client.users.get(author).discriminator));
                if (body.player.socialMedia.links["DISCORD"] === (client.users.get(author).username + "#" + client.users.get(author).discriminator)) {
                    fs.writeFile(`users/${body.player.uuid}.json`, `{"uuid":"${body.player.uuid}",\n"ign":"${nm}",\n"id":"${author}"}`, {"flag": "w"});
                    successFlag = true;
                }
            }
        }
    });
    console.log(successFlag);
    return successFlag;

If I use async/await I am unsure if input will continue to be processed while the data is fetched.

  • 1
    For a deep discussion on what you're trying to do check out this other answer https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call I've flagged this as a duplicate, it's a very commonly asked question and I hope you can get some clarity from the above linked discussion. – Jon Church Mar 03 '20 at 05:17
  • If I await the request to be completed before I return, will events still trigger while the request is handled? – Ender_The_Xenocide Mar 03 '20 at 07:07

1 Answers1

0

The return successFlag; should be inside the callback, else without getting a response, it would be returned. So change the last 3 line to

    console.log(successFlag);
    return successFlag;
});
Abishek Kumar
  • 519
  • 5
  • 13
  • That still won't work. Returning from the callback to request will not make the code work synchronously. The enclosing function that request is being called from will still exit. – Jon Church Mar 03 '20 at 05:12
  • So the enclosing function has more return within it? – Abishek Kumar Mar 03 '20 at 05:14
  • 1
    You cannot return directly from an asynchronous operation. See the linked answer in the comment on the question for more information – Jon Church Mar 03 '20 at 05:20
  • yes, you can return from asynchronous operations, the thread is for client-side and that too says if the response time is too long then avoid it, but since we are only blocking the response related operation, I think that's the correct way. – Abishek Kumar Mar 03 '20 at 05:33
  • @Ender_The_Xenocide Please provide the use-case or the complete dummy function which is calling the request. – Abishek Kumar Mar 03 '20 at 07:59
  • If you follow your steps, the function will return undefined and the return value would be useless – Ender_The_Xenocide Mar 03 '20 at 08:09
  • Since the request function is async, the function that contains it will just skip over it and return – Ender_The_Xenocide Mar 03 '20 at 08:10
  • that's what currently is happening in your code the function will return undefined, but what I am asking you to do is to move the return in the callback function, so it is called after receiving the response. – Abishek Kumar Mar 03 '20 at 08:53