Question:
What is a Promise?
Quick answer:
It is an object which represents the current state and value of the operation. There are three states f...
For further actions, you may consider blocking this person and/or reporting abuse
I have a question.
How do onResolve , onReject and onFinal methods at the constructor wait for all 'then' or 'catch' methods to be called ? You set the timeout to 0 and invoke 'func', 0 means run the code immediately.
Hey π Great question!
For example, if you use
setTimeout, your function will be put into the execution queue, but not executed right away.will produce output
even though
console.log(3)appears beforeconsole.log(1)andconsole.log(2).MDN/Even toop
not precisely it means "run code in 0 seconds or more". This is because when running code with settimeout this is handled by timer API. Timer API waits given time (here 0 seconds) and then puts function (code that was contained within settimeout) in the message queue. Elements from message queue are run only if event stack is empty (this is far more complex than that and to get it I'm recommending this talk, guy does great job at explaining the event loop and js execution). This is why settimeout is sometimes used as an hack to run our code after call stack is empty (running currently functions ended the execution)
hey Nikita,
Thanks for sharing such a great article,
one more thing I suggest to replace setTimeout with queueMicrotask, since then/catch/finally callbacks is a microTasks and not Tasks as setTimeout callback.
in such a case promises callback will be passed by JS engine into the microTask queue.
Thanks for sharing
::)