I'm trying to figure out how this function is working depending if it's called inside a setTimeout or not.
var myFunc = function(){
console.log('function executes');
return function(){
console.log('return');
}
}
then if I call
var exe = myFunc();
it logs 'function executes' in the console
but if I call
var exe = myFunc();
setTimeout(exe,1500);
I got 'function executes' and after 1500ms 'return', but not 'function executes' again, so just the code inside the return part of the function is getting executed.
Can anyone explain this behavior??
var exe = myFunc;(without parentheses) to see a different behaviour, maybe that helps you understand what happens. (What you see in the code of your question is perfectly explainable, it's the expected result)