This is a great writeup, wish I had it when I started with Flutter. BTW, I've written a couple Flutter apps now and this set of tools (Futures, async/await, Isolates) covers my multithreading use cases in a really nice and safe way.<p>By comparison, Grand central dispatch is pretty nice on iOS, but I really miss the first class async/await calls. Side note: How did they make an entirely new language for app development (Swift) and not build in async/await support?<p>Kotlin co-routines seem to be a pretty nice solution on Android, but before that it was a bit of a mess. There was 4 or 5 different ways to do things async, they all interact in super complicated ways with the activity life cycle, and basically nothing is quite what you need.<p>Side side note: Unity also does a really nice version of all of this with a single event loop and really powerful co-routines. Worth checking out sometime.
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.)<p>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.<p>Basically,<p>"`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"<p>If I have the above wrong someone please call me out.<p>Great article.
I'm no functional fanatic, but one thing that I enjoy about it is the idea that function calls- and futures- resolve to a value.<p>The code example where D doesn't get called last because of the missing await grinds my gears something fierce, especially because I missed it the first glance through. This type of bug is much harder to create when you don't constantly rely on side effects everywhere.<p>That said, the article did a really good job if you're not already familiar with the same concepts from JS land (with the event loop sans visible microtasks, Promise instead of Future, abd web worker instead of Isolate).
If you're interested in the Dart VM, I also recommend taking a look at Vyacheslav Egorov's Introduction to Dart VM (<a href="https://mrale.ph/dartvm/" rel="nofollow">https://mrale.ph/dartvm/</a>).
What is the role of the `async` keyword in Dart? I couldn't understand it from the article.<p>> the outcome of the method is a Future<p>Isn't the function signature sufficient to express this?<p>> it runs synchronously the code of that method up to the very first await keyword<p>How is this different from non-async methods?<p>> the next line of code will be run as soon as the Future, referenced by the await keyword, will have completed<p>But the await keyword is in the caller which is not statically known.<p>What is the difference between an async method and a method that returns a Future?
A typo:<p><pre><code> while (microTaskQueue.isNotEmpty){
...
return;
}
</code></pre>
It should be either "if { ... return }" or just "while { ... }".
For someone used to work with async/await only in C#, I was not expecting the execution model to be so fundamentally different in Dart.<p>Something to try to wrap my head around later!