3 ms·
It works like in every other language that supports async/await. Compiler will convert code inside async method into a state machine to prevent it from locking
by krzat 8y ago
It works like in every other language that supports async/await. Compiler will convert code inside async method into a state machine to prevent it from locking thread during await calls.
If you don't uses async you have to do compiler's job yourself.
- ridiculous_fish 8y agoBut why do we need a new keyword to express this? Isn't the return type sufficient?
- krzat 8y agoNo, because you can write async functions without using async/await. Using callback for example: Future<int> foo() { return Future.delayed(Duration(seconds: 1)).then((x) => 1); } In this case there is no compiler magic needed. If you ask why async keyword is needed if we can just infer it from await inside the method body - probably only for readability.
- ridiculous_fish 8y agoasync is part of the method signature, the public facing part of the method. But it controls an implementation detail, namely whether the function body may use await (as you say asynchronous functions are not required to use await). I found a link [1] that described async as a compatibility affordance in C# 5: legacy code may use await as a variable name, so async opts into the contextual await keyword. But Dart doesn't have such legacy, so the keyword doesn't seem as well motivated. https://blogs.msdn.microsoft.com/ericlippert/2010/11/11/asynchrony-in-c-5-part-six-whither-async/ https://blogs.msdn.microsoft.com/ericlippert/2010/11/11/asyn...