A
A
adrenalinruslan2019-05-08 14:51:40
JavaScript
adrenalinruslan, 2019-05-08 14:51:40

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

2 answer(s)
A
Anton Spirin, 2019-05-08
@rockon404

JavaScript: Asynchronous Programming Techniques

S
Stalker_RED, 2019-05-08
@Stalker_RED

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 question

Ask a Question

731 491 924 answers to any question