Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 14, 2021 06:56 pm GMT

Javascript runtime interview question

I would definitely put the following question in an interview, if am interested in knowing if the candidate knows a bit about the Javascript runtime.

What is the order in which the following texts are logged via console.log?

console.log('1 - start');setTimeout(() => console.log('2 - setTimeout1'), 0);Promise.resolve('Success')    .then(()=> console.log('3 - promise1'))    .then(()=> console.log('4 - promise2'));setTimeout(() => console.log('5 - setTimeout2'), 0);console.log('6 - end');

If you are not sure about the answer read this In depth: Microtasks and the JavaScript runtime environment article before

Output

1 - start// statement is executed directly in the script (first "Run script" task)5 - end // part of the first "Run script" task gets executed too3 - promise1 // placed in microTasks queue and executed between tasks, after first "Run script" task is ready4 - promise2 // microtask added  during previous microtask execution  and executed immediately2 - setTimeout1 // callback execution scheduled in another task in the "macroTasks" queue (also task queue), executed in the next interation of the event-loop5 - setTimeout2 // callback execution scheduled in another task in the task queue, executed in the next iteration of event-loop

Shared from Codever. use the copy to mine functionality to add it to your personal snippets collection.


Original Link: https://dev.to/codever/javascript-runtime-interview-question-5bo8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To