3

function f() {
  const color = "red";
  setTimeout(() => console.log(color), 1000); 
}

f();

In this code, setTimeout's callback (() => resolve(color), 1000) is asynchronous and is put on the macro-task queue after 1 second. When the call-stack is empty, there is a chance for the callback to be pushed onto it.

I thought to myself, when this callback is executed on this 'refreshed' call-stack, it remembers everything it had access to when it first appeared on the call-stack during 'initial execution of script'. For instance, it can access color. How is this possible? Is this the result of 'closures'? Does a deep-clone 'copy' of that function appear on the call-stack later and it can access all what it could access where that function was first defined? At this point I'm just guessing but I thought it a worthy question.

tonitone120
  • 1,920
  • 3
  • 8
  • 25
  • 3
    1. calling `Promise.resolve` on a function will create a Promise with the value being the *function*, not the result of calling it. 2. What is "the WebAPI section"? 3. that isn't really how the call stack works. 4. There is no 'result' property. 5. There is no "main-stack". 6. If the callback is async, you don't have to wrap it in a Promise as it will return one automagically. It isn't at all clear what you are asking here, so it's difficult to understand how to answer it. – Jared Smith Sep 07 '20 at 22:38
  • Read up on [Closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) – NineBerry Sep 07 '20 at 22:40
  • Lots of articles around regarding how promises work under the hood – charlietfl Sep 07 '20 at 23:03
  • 1
    What do you mean by "*gets put to the WebAPI section*"? – Bergi Sep 07 '20 at 23:04
  • "*When asynchronousCallback1 is executed*" - uh, in the snippet `let result = Promise.resolve(someAsynchronousCallback1)` you posted, the `someAsynchronousCallback1` is never getting executed at all. – Bergi Sep 07 '20 at 23:05
  • 1
    Now that you've updated the code, the text of the question doesn't fit to it. There's no `result` and no `asynchronousCallback1` any more. – Bergi Sep 07 '20 at 23:49
  • @Bergi Does it make more sense now? – tonitone120 Sep 07 '20 at 23:50
  • @tonitone120 much, much better, I've voted to reopen it. I suggested that you look at a Promise polyfill to see how it works. – Jared Smith Sep 08 '20 at 00:58
  • @Bergi Do you think it's worthy of re-open now? – tonitone120 Sep 08 '20 at 16:09
  • I'd probably now close it as a duplicate of [Is the Promise constructor callback executed asynchronously?](https://stackoverflow.com/q/29963129/1048572) - there are actually no microtasks involved in this code snippet, only the `setTimeout` macrotask. – Bergi Sep 08 '20 at 18:29
  • And I'm not sure about the "*how can something that was once on the call-stack have its value changed by an entirely different function that appears later on the call-stack?*" remark. It's the same as in a mundane `const obj = {value: null}; setTimeout(() => { obj.value = 'result!'; })`, just that `resolve` is modifying internals of the promise object instead of modifying a normal object property. – Bergi Sep 08 '20 at 18:32
  • @Bergi You're right that is what I'm getting at. Sorry this question took so long to finalise. What do you think of it now if you wouldn't mind sparing your thought? – tonitone120 Sep 08 '20 at 19:44
  • Where is `resolve("done!")` in the code snippet? – Barmar Sep 08 '20 at 20:44
  • @tonitone120 Now it's just [How do JavaScript closures work?](https://stackoverflow.com/q/111102/1048572). The timeout is not even relevant for that, you also have no `color` on the *call stack* in `let g; function f(){ const color = 'red'; g = () => console.log(red); } f(); g();` – Bergi Sep 08 '20 at 22:56
  • @Bergi I'm observing, what seems to me, strange behaviour and I'm guessing closures have something to do with it. I'm sure you could confirm to me it's something to do with closures or point me in the right direction? – tonitone120 Sep 08 '20 at 23:05
  • 1
    Yes, this is plain closures behaviour, nothing strange about it. – Bergi Sep 08 '20 at 23:10
  • @Bergi Okay, thank-you – tonitone120 Sep 09 '20 at 00:12

0 Answers0