Is there any difference in the order of execution?
In Dart we also have asynchronous events that are handled by
dart:async. There we can distinguish between instants and cycles. An instant is composed of one or more cycles. An instant executes all its cycles until there is none left and then moves to the next instant. DOM events are at the same level as instants. That is, a DOM event will never be interleaved with cycles of the same instant. [^1] (This also means that piling up cycles within the same instant can starve the DOM.)
runAsync queues a new cycle.
Timer.run queues a new instant.
Futures and Streams use cycles to queue events that can be executed immediately. In the following example both
thens will be scheduled within the same instant and therefore run before any DOM event has the chance to interfere.
var future = new Future.value(499); future.then(print); future.then(print);
window.setTimeout is not allowed to execute the computation before 5ms. (This is due to unfortunate historic circumstances). Dart doesn’t have this restriction. [^2]
We are currently discussing changes to the library to guarantee that
Timer events are executed in the “correct” order and not just after the waiting-time has elapsed.
[^1] This is not correctly implemented. As of May 2013 ever asynchronous operation is built on top of
[^2] This, too, is not done yet (May 2013). I’m not sure about Dartium, but dart2js currently still uses
window.setTimeout. It will eventually switch to newer primitives (on browsers that support it) that allow for more precise timeouts.
Answered By – Florian Loitsch
Answer Checked By – Clifford M. (FlutterFixes Volunteer)