Answer the question
In order to leave comments, you need to log in
How to set the order of execution?
How to set the order of execution of JS code? I run the script through the console.
The problem is that the function does not have time to complete the work, and undefined is already written to the variable.
How to fix this?
How to do it like in PHP, so that while let's say this function does not work completely, then the following code will not run
var cRequest = function(url)
{
request(url, function (error, response, result)
{
if(error === null && (response && response.statusCode) === 200)
{
console.log(result); // success result
return result;
}
else
{
return {
error: error,
response: response,
result: result,
};
}
});
};
setInterval(function()
{
var liveGames = cRequest('https://google.com/');
console.log(liveGames); // undefined
}, 1000);
Answer the question
In order to leave comments, you need to log in
No need to "fix" it, because it's designed that way on purpose.
Read the tutorial about asynchrony . There are more ways than one.
Well, or rewrite request for synchronous work, but then don't complain.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question