Unable to understand chain of then function of promise

in the video when we are calling functions as:
hellosayer(3,‘Arnav’).then(hellosayer(2,‘Prateek’).then(hellosayer(3,‘Rishab’)))
why is the output not sequential as we are calling a
promise only after ending of previos promise.

Send the entire code you are talking of…

the code is in the video only and the whole code is not available on the screen at any time


the caling of functions is as i have mentioned earlier

Have to tried running the code on your system?

Sir ran the code in video itself and after your saying i also tried but all three promises are running parallely

Ok, now send your code… I will explain the reason behind it…

Sir please reply.Why is the output parallel and not seqential

Before understanding the behavior, go through the following article:

  1. Execute hellosayer(3, ‘Arnav’)
  2. Interval set for execution after 100 millisecs.
  3. After 100ms, print “Arnav”, resolve the promise, and set interval for 100ms (call pushed to callback queue)
  4. Since promise was resolved, hellosayer(2, ‘Prateek’) is executed.
  5. Similar step 3 performed for Prateek.
  6. hellosayer(2, ‘Rishab’) is executed.
  7. Similar step 3 performed for Rishab.
  8. Call stack is empty, Event loop pushes callback from callback queue to call stack in FIFO.
  9. Because of this Arnav, Prateek, Rishab is printed in order again.
  10. Same happens for 3rd time, but this time Prateek is not printed because its clearInterval was called after 2nd execution.

but resolve is only called after count===time i.e. when clearInteral is also called so why is promise resolved after printing everytime.

Resolved in not bound to that condition. It’s outside the if.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

how is resolve outside that if.I mean both clearInterval and resolve are inside parenthesis of if block.