There is a method in Javascript where can I execute the code after returning?
That's right. It was called setTimeout() , but for some reason I doubt it would be a good solution for you.
One way or another:
var foo = (function(){ var b; return { bar: function(a) { if(b){ setTimeout(function() {b = false;},20); return b; }else{ b = a; return false; }; } }; })(); foo.bar(1);
The function that is passed as the first argument to setTimeout will "close around" the b variable, and set it after 20 milliseconds.
If you want to keep a synchronous stream of code execution, then absolutely not, unless you do it manually through a function that returns along with the desired value.
Ultimately, temp best option. You can close it, like the b variable, if you want:
var foo = (function(){ var b,temp; return { bar: function(a) { if(b){ temp = b; b = false; return temp; }else{ b = a; return false; }; } }; })(); foo.bar(1);
source share