R
R
r2sada2r2015-05-03 19:12:47
Node.js
r2sada2r, 2015-05-03 19:12:47

How to achieve waiting for the result of a function in an if statement?

function testFunc() {
  async.series([
    function(down) {
      setTimeout(function(){
        down();
      }, 10000);
    }
  ],
  
  function() {
    return true;
  });
};

if(testFunc()) console.log('Result function: true');
else console.log('Result function: false');

After running, I get: Result function: false
It is understandable, because return true; in testFunc, will come only after 10 sec.
How to make an if statement wait for the result of the testFunc function?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vlad Lavrik, 2015-05-05
@lavrok

You can also make it look completely synchronous with the help of generators (plus promises).
You can already safely use it in a node, plus getting rid of unlimited nesting and crap debugging.
habrahabr.ru/sandbox/67590

V
Vladlen Grachev, 2015-05-03
@gwer

If it is simpler and without callbacks, then move this operator inside the function. Well, in general, google "Asynchronous JS".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question