Skip to content
tezvyn:

Describe the Dart event loop and queue execution order

Source: dart.devMediumHow cards are made

Describe the Dart event loop and queue execution order

Tests your grasp of Dart's single-threaded event loop priority. Great answers state: microtasks drain fully before the next event processes, cycling forever. Red flag: saying Future and scheduleMicrotask interleave in call order.

What's really being asked

The interviewer is checking whether you understand Dart's single-threaded concurrency model at a deep level. They want to see that you know an isolate runs one event loop with two distinct queues, and that you can predict which asynchronous work runs first based on queue rules rather than call order alone.

The full answer

First, state that every Dart isolate has exactly one event loop and two queues, the microtask queue and the event queue. Second, explain the execution rule: after any synchronous code completes, the loop always drains the entire microtask queue before it processes even a single event from the event queue. Third, clarify that scheduleMicrotask adds work to the microtask queue, while Future and timers add work to the event queue. Fourth, describe the cycle: drain all microtasks, process one event, then repeat. Fifth, note that microtasks are intended for small internal asynchronous completions that must happen before the next event, while events handle I/O, timers, and user interaction.

The mistakes people make

A critical red flag is claiming that tasks run in simple call order without respecting queue boundaries, which would mean a Future called before a microtask runs first. Another mistake is conflating microtasks with events or saying Future.value always goes to the event queue. Some candidates incorrectly bring up threads, thread pools, or the JavaScript event loop without acknowledging Dart's stricter microtask-draining semantics. Saying the queues have equal priority is also wrong.

What usually comes next

The interviewer may ask what happens if a microtask recursively schedules another microtask. The correct answer is that the loop continues draining microtasks until the queue is empty, which can starve the event queue. They might ask where Future.delayed, Stream listeners, or user gestures land, which is always the event queue. They could also ask how this relates to the build method in Flutter, where setState triggers a microtask-like frame callback but the principle of queue priority still matters for post-frame callbacks.

A concrete example

Imagine a main function that prints sync start, then calls scheduleMicrotask with a callback printing microtask A, then calls Future with a callback printing event A, then calls scheduleMicrotask again with a callback printing microtask B. The output order is sync start, microtask A, microtask B, event A. This occurs because both scheduleMicrotask calls populate the microtask queue, and the event loop empties that entire queue before pulling the Future callback from the event queue. If the candidate cannot produce this ordering, they do not understand the queue priority.

Interview question

In main(), sync prints 1 and 6 surround a Future(2), microtask(3), Future(4), and microtask(5). What is the output order?

  • a.1, 3, 6, 5, 2, 4
  • b.1, 6, 3, 2, 5, 4
  • c.1, 6, 2, 3, 4, 5
  • d.1, 6, 3, 5, 2, 4Correct
Why?

The event loop finishes all synchronous code first, then fully drains the microtask queue before processing any event queue callbacks, yielding 1, 6, 3, 5, 2, 4. Option B is tempting because it follows call order, but Dart strictly processes entire microtask batches before touching events.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles