1

I want do something like that:

var x = function(){
  if (controlVar === 0) {
    setTimeout(x, 300);
  }
  else {
    return value;
};

Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1?

This should not block, because when I use setTimeout the main loop shoul be able to take the control, but if block doesn't matter for me.

Thanks.

Fi3
  • 429
  • 6
  • 17
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Zakaria Acharki Mar 22 '16 at 17:31
  • Sorry, this must not block, because if block my controlVar can't turn in 1. – Fi3 Mar 22 '16 at 17:32
  • Please try to describe what you're trying to achieve and not just the problem. – Zakaria Acharki Mar 22 '16 at 17:34
  • For clarify I want call a recursive function that return only when a variable that act like a semaphore turn green. And I want call this function like that 'var x = f(z)' – Fi3 Mar 22 '16 at 17:36
  • No. If you don't want to block (and you don't want to), you can't `return` the value from asynchronous callback. – Bergi Mar 22 '16 at 17:46
  • You'll probably want to look at AJAX and WebWorkers as potential solutions for this, but it's not really possible with basic JavaScript given that it runs to completion. – ManoDestra Mar 22 '16 at 17:48

1 Answers1

1

Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1?

No. Assignment is synchronous. It can't wait for an asynchronous function. To be more precise, x immediately returns, it doesn't wait for the timeout.

setTimeout simply adds a new job to the job queue. Only after the current job is finished, the next job will be processed.

I want call a recursive function that return only when a variable that act like a semaphore turn green.

That doesn't work in JavaScript because it has the concept of "run to completion":

Each message is processed completely before any other message is processed. This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it can be stopped at any point to run some other code in another thread.

Going back to your example, we start with evaluating the script and calling x. This is the current status the job queue:

Job Queue--------------------------------------------------+
| +---------------------+                                  |
| |What: evaluate script|                                  |
| |                     |                                  |
| |                     |                                  |
| +---------------------+                                  |
+----------------------------------------------------------+

When the line setTimeout(x, 300); is executed, a new entry is added to the job queue:

Job Queue--------------------------------------------------+
| +---------------------+    +-----------------+           |
| |What: evaluate script|    |What: execute x  |           |
| |                     |    |When: 300ms      |           |
| |                     |    |                 |           |
| +---------------------+    +-----------------+           |
+----------------------------------------------------------+

In order for the the next entry to be processed, the current job must be completed first. That means, since we are currently calling x, x must terminate. Only after x returned, the event loop can move on to the next job and call x again.

Hopefully this makes it a bit clearer why x cannot wait for the timeout.


For alternative solutions see How do I return the response from an asynchronous call? .

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143