![]() We have one entry in TaskQueue and one in JobQueue. In the following execution line, the function jerry is pushed into the Stack and executed. ![]() Hence the console log function execution on the promise goes to JobQueue. A callback of a promise gets a place in JobQueue. In the following line of execution, we encounter a Promise.The setTimeOut web API goes outside of the Call Stack in the following execution line, and the associated function tom gets placed into TaskQueue. It executes the console log of the text Cartoon.The function cartoon gets into the Call Stack.Should it be right after Tom, before Jerry? Resolve( 'should it be right after Tom, before Jerry?') const tom = () => console.log( 'Tom') Ĭonst jerry = () => console.log( 'Jerry') Let us understand the above logic with a Code execution flow. Overall, the Event Loop got one more item to consider in its orchestration of Code Execution. At any point in time, if both the queues got entries, JobQueue gets higher precedence than TaskQueue.The entire microtask(Job) queue is completed before the 'event loop' looks into the next thing. Once that task is complete, the event loop visits the microtask(Job) queue. ![]() For each loop of the 'event loop', one macrotask(Task) is completed out of the macrotask(Task) queue.The obvious question would be, how does the event loop decide which queue to de-queue from and push to Call Stack when the Stack is empty? The answer depends on this logic(or, set of rules): With the explanation above, let us re-visit the Event Loop Model once more than last time. The Queue in Event Loop Model holds the Jobs(or the MicroTasks) called, JobQueue.įor example, Promises are in Job Queue, and the functions for setTimeOut are in TaskQueue.The Queue in Event Loop Model holds the Tasks(or the MacroTasks) called, TaskQueue.Examples of Microtasks are, Promises, processes.nextTick, etc.Examples of macrotasks are setTimeout, setInterval, setImmediate, I/O tasks, etc.MacroTasks are called Tasks, and MicroTasks are called Jobs.Not all the tasks are created of the same priority. ![]() To recall, some of it already explained here that there is Task Queue in the Event Loop Model. However, this particular post is about understanding what goes under the hood when a promise gets executed? Along with it, we will also learn the difference between Task Queue and Job Queue. Interestingly, this post is mostly about How to keep a Promise and execute it.īefore we move further, I would like to clarify that I will not explain Promises as a concept here. In my last post, I also made a promise that I shall write about the concept of Task and Job Queues. * Optional file path which needs to be included for $fucntion.Keep every promise you make and only make promises you can keep. * Optional array of arguments to pass to the function. * A human-readable description of the queued job. The function added will be called in the order it The job_queue_add() function is used by modules to add a job to the queue: /** This module is used by modules to queue function calls for execution on cron. This is end of lifed for Drupal 7, there is a queue in core.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |