Getting response with NodeJS request module

Yes. request is an async function so while it is waiting for a response from the server, the rest of your code will keep on executing, in this case your callback.

move this

if (streamers.length < 0){
    callback("ERROR");
}else{
    callback("SUCCESS got streamers " + result);
}

inside of the callback being passed to request.get, this will mean that if-else statement wont get run until request receives the response.

And in your current code, make sure to wrap the JSON.parse() in a try { ... } catch(err) { } because there will be times where the response is malformed, an error, etc… and this can kill a program if you’re not catching it.

1 Like