JavaScript – Wait for execution

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()

Leave a Comment