How does JavaScript know when the Promises callback function is ready to execute?

I know that there is a message queue and that JavaScript is single threaded. And also an event loop that receives messages from the queue and does some work.

When you create a promise, and this promise is stored in the message queue when or how the loop knows when it is resolved (or rejected). Is the question constantly asked whether the status is β€œpending” or is the promise of a message queue itself if resolved or rejected?

Update:

After some research, I think I was wrong. This is not a promise itself, but an XHR request that actually blocks and runs in its thread so as not to block the main thread of execution. Thus, only the main thread of execution is single-threaded, and the XHR thread will place callbacks in the main thread when the XHR request receives the result (or does not work).

So, the corresponding article , the promise itself is stored on the stack and will call the callback function in the queue when it is allowed?

+4
source share
3 answers

So, the corresponding article , the promise itself is stored on the stack and will call the callback function in the queue when it is allowed?

, nem035:

, , . , , .1

, :

the promise is then added to the Microtasks queue

, , , Promise then (callback) Microtasks, , .


1https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

+1

, .
, .

+1

, JavaScript . , .

, , , ( ). , "" ?

" ", -. promises. .

When a promise is fulfilled or rejected, the promise library queues tasks for all attached handlers thenor catch. (If the handler is theneither catchattached after fulfilling or rejecting the promise, the task is immediately queued.)

0
source

Source: https://habr.com/ru/post/1676671/


All Articles