@byles1506 This is a tricky exercise that explores concepts like the task queue, microtask queue, and how the event loop works.
When the program runs, await funcOne
will be called (and placed inside the microtask queue), and it will immediately output "A" because this is a synchronous operation. Next, the IIFE will execute. At the await funcTwo()
line, funcTwo runs and completes immediately. However, the .then
callback of the promise is not executed right away. Instead, it is placed in the microtask queue.
The program will then move on from the IIFE without waiting (because don't have any await to hold it) for its result to complete and proceed to the next synchronous task, which is logging "C." At this point, the execution of funcOne finishes, and the program continues after await funcOne();
, logging "Done." because is the next synchronous operation.
Once all the synchronous tasks are finished, the event loop will process the microtask queue. This is when "B" will be logged.
In this link, you can have a more detail explanation about those three concepts.