This gist contains Dart and JavaScript examples of using the microtask queue to implement a recursive breadth-first search. Normally, a recursive graph/tree search is depth-first, as it uses the call stack to keep track of the frontier. However, the event loop in JavaScript and, by extension, Dart, uses a queue, not a stack. The microtask queue in particular is good for a recursive pattern as it blocks up the entire event loop until every single microtask has been processed.
Interestingly, Dart actually gives much simpler control over which queue things go into. In JavaScript, Promise
s, and thus async
functions, always resolve in the microtask queue, not the regular event queue, which is why the JavaScript implementation does not require anything extra to deal with which queue things get sent to. In Dart, however, Future
s normally complete in the normal event queue, not the microtask queue,