0

I know Promisses but i have a little problem. You know when we create an instance of Promise, the executer func goes in to the job queue and then the function will be pushed in the callStack and then will be executed. And you know when we want to use the resolved or rejected value, we should use .then() or .catch() and these get a callBack func. Here is my question. will the callBack that we pass to .then() or .catch() be pushed in to the callBack queue and then it will be executed in callStack or not? What happens to .then() if not? Thanks for helping.

saman
  • 31
  • 5
  • "*the executer func goes in to the job queue*" - [no, it doesn't](https://stackoverflow.com/q/29963129/1048572) – Bergi Dec 27 '21 at 20:05
  • I'm not sure what you mean by "*then it will be executed in callStack or not? What happens to .then() if not?*". Notice that `.then()` is usually called on the promise *before* `resolve` is called. – Bergi Dec 27 '21 at 20:08
  • hello. let me say my mean step by step. does callBack passed to .then() run like these steps? 1- the callBack will be push in the callBack queue. 2- the callBack will be pushed in callStack suddenly. 3- the callBack queue will be executed. or – saman Dec 27 '21 at 20:20
  • It depends on whether the promise is already settled or not. If it's fulfilled, yes, `then` will schedule the promise job. Otherwise it will just attach the handler to the promise, so that it can get schedule when the promise is getting resolved. – Bergi Dec 27 '21 at 20:21
  • Not sure what you mean by "*the callBack will be pushed in callStack suddenly*". It doesn't happen randomly, it happens when the currently executing code (whatever that is) finishes (the callstack becomes empty) and the next job from the queue is serviced. – Bergi Dec 27 '21 at 20:23
  • yes i know that. i mean what happens to the callBack that we pass to .then()?? will event loop take the callBack from .then(), then push that in to callBack queue then push that in to callStack or event loop take the callBack from .then() and then push that in callStack without pushing that in callBack queue? – saman Dec 27 '21 at 20:25
  • i know how executer function that we pass to Promise constructor will be executed. but my point is not this. my point is the callBack that we pass to .then() or .catch(). – saman Dec 27 '21 at 20:29
  • The event loop only ever de-queues (and executes on the call stack). It's `then` (or `resolve`) itself that pushes the callback into the queue; the event *loop* doesn't really know (or care) about that - it only looks whether there is something to dequeue or not. And yes, asynchronous callbacks (like those of promises) need to *always* go through the job queue, if `then` (or whatever) would directly call them (put them directly on the call stack) they would be synchronous. – Bergi Dec 27 '21 at 20:29
  • i want to know how does the passed callBack to .then() or .catch() execute? it first go to callBack queue and then callStack, or it go to callStack without going to callBack queue or... – saman Dec 27 '21 at 20:32
  • oh i got it now! so the passed callBack to .then or .catch goes to callStack directly without going to callBack queue. right? – saman Dec 27 '21 at 20:35
  • No, that's the opposite of what I wrote. – Bergi Dec 27 '21 at 20:52
  • Hah i made a mistake :) – saman Dec 27 '21 at 20:55

0 Answers0