-5

I have the code below:

function foo() {
    setTimeout(function() {
        console.log("a");
    }, 0);
    console.log("b");
    console.log("c");
}

In my console, I have the result below:

b
c
undefined
a

I need get the result below:

undefined
a
b
c

The commands to print "b" and "c", need stay in the root foo function.

How to do it?

-

The case above simplifies my need.

FabianoLothor
  • 2,752
  • 4
  • 25
  • 39
  • 1
    Just put `b` and `c` in the same function as `a` – Bergi Jun 10 '14 at 17:09
  • 1
    Is there a point to the setTimeout? – j08691 Jun 10 '14 at 17:09
  • 1
    No matter how short the timeout is, the function won't run until the script finishes and you return to the main event loop. – Barmar Jun 10 '14 at 17:10
  • @j08691 setTimeout represents one ajax fucntion assync, and I need return (in main function) one boolean because, I using TypeScript and the return need stay in main function. – FabianoLothor Jun 10 '14 at 17:13
  • I need something like sleep function between the commands. – FabianoLothor Jun 10 '14 at 17:15
  • 2
    @FabianoLothor You have to use the events and callbacks associated with an asynchronous task to control execution timing. The script simply doesn't execute strictly top-to-bottom when you involve anything asynchronous as they are very intentionally "*outside of the normal flow*." – Jonathan Lonowski Jun 10 '14 at 17:17
  • @JonathanLonowski I know, the problem is: I using TypeScript and the language not compile when I put the return in callback function. You understand? – FabianoLothor Jun 10 '14 at 17:20
  • 1
    @FabianoLothor Well, it's not possible to `return` the result of an asynchronous task. [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) You have to write your code in a different manner and TypeScript can handle callbacks and promises as well. – Jonathan Lonowski Jun 10 '14 at 17:21
  • @FabianoLothor then you should update your question to state that you need to return a result from timeout – Andrew Vermie Jun 10 '14 at 17:24
  • Read upon [What do I do if I want a JavaScript version of sleep()?](http://stackoverflow.com/q/951021/1048572) and [Why is my variable undefined after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/1048572). Not sure if I should close it as a duplicate. – Bergi Jun 10 '14 at 17:27
  • Where did you got `undefined` ? – Ingmars Jun 10 '14 at 17:44
  • @Ingmars: `undefined` is the return value of `foo()`, when that is evaluated from the console. – Bergi Jun 10 '14 at 18:08

1 Answers1

-2
function bar(){
    that = this;
    this.printed = undefined;

    this.foo = function() {
        if (typeof this.printed === "undefined") {

            this.printed = false;

            console.log("a");

            setTimeout(function() {
                that.printed = true;
                that.foo();
                return undefined;                
            }, 0);
        }


        if(this.printed == true) {
            console.log("b");
            console.log("c");
            this.printed = undefined;
        }
    }
}

new bar().foo()
Monarch Wadia
  • 4,400
  • 4
  • 36
  • 37
  • No. Don't do that. This function cannot run in parallel to itself, and behaves erratic when one tries to. – Bergi Jun 10 '14 at 17:30
  • @Bergi OP never asked for multiple instances to be run. – Monarch Wadia Jun 10 '14 at 17:31
  • 1
    That's one of the implicit features of an asynchronous function. OK, sometimes one might want to *lock*, but not like this. "Recursively" calling itself when there is no recursion required is confusing and should be avoided. – Bergi Jun 10 '14 at 17:36
  • The output is as follows for me: `Unhandled Error: 'this.foo' is not a function` - even worse. Apart from that, making `foo` a method and `printed` a non-global property still doesn't solve problem. I'm not sure why you deleted your first answer, it was fine! – Bergi Jun 10 '14 at 17:47
  • Fixed! Please check again, the output is proper for me, and it now handles multiple parallel calls as well. – Monarch Wadia Jun 10 '14 at 18:01
  • no. Apart from [`that` being global](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html), `var x=new bar(); x.foo(); x.foo();` still does not work. – Bergi Jun 10 '14 at 18:06