3 ms·
I've been getting into Flutter and Dart recently after being a long time React fan. I must say I like many things Flutter does differently. Especially around it
by pests 8y ago
I've been getting into Flutter and Dart recently after being a long time React fan. I must say I like many things Flutter does differently. Especially around its Material UI package (built-in) which is miles above any other web material component library in any framework (React, Vue, Angular, etc.)
I must say wrapping my head around the addition of 'await' and the changes it made to the EventLoop processing was difficult but as I type this comment I realize I do fully grok it now. I must say that this
is IMO probably the most important piece of info in the article.
Basically,
"`await` causes immediate execution of the Future (up to the first `await` in the Future) verses appending the Future to the end of the EventLoop queue"
If I have the above wrong someone please call me out.
Great article.
- tylerhou 8y agoAwait doesn’t immediately execute the future; see the example code in the article. It looks like the Dart concurrency model is basically the same as JS except with microtasks and replacing “worker” with “isolate.”
- pests 8y agoI'm not sure I agree. In the first example `await` is added before the Future creation and this causes immediate execution in the printed output (second version). In the first version both Futures are pushed onto the EventTask queue and are executed after main() has finished. In the second example with method1 and method2, in method1 with async (which signals a Future) on the forEach, all three Futures are pushed to the EventLoop queue and execute after method1() has finished. In method2() there is no new Future being added and execution happens immediately with the end of method2 executing after all delayedPrints. You also see the effect with all three Futures resolving at once in the method1 case due to all three being added to the queue at the same time each with the same delay. Whereas with method2 each print with a delay after due to this function being synchronous with nothing going on the EventLoop.
- tylerhou 8y agoAwait pushes your task onto the end of the event queue; this might seem like you’re immediately executing a future in many cases but it’s not true in all cases. Other futures created by your thread might take priority. Consider the case where you pushed a future onto the event loop which infinite loops before you used await. Then your code will freeze.
- KingMob 8y agoUm, microtasks are in the Js/browser model, too: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/ https://jakearchibald.com/2015/tasks-microtasks-queues-and-s...
- tylerhou 8y agoDidn’t know that, thanks