If you don't uses async you have to do compiler's job yourself.
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.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/asyn...
There's no difference for callers of the method, but you can only use await in methods that are async.
For example:
return 4;
If the function is marked async, the return value should be Future<int>.If you allow a non-async method use await, you get into this situation:
Future<int> foo(){
int result = await something ();
// How to return "result" and still make compiler happy?
}It sounds like the async keyword allows for a particular implicit type conversion within the function body, but on return statements only. That's a pretty thin justification for a keyword.
> the outcome of the method is a Future
Isn't the function signature sufficient to express this?
> it runs synchronously the code of that method up to the very first await keyword
How is this different from non-async methods?
> the next line of code will be run as soon as the Future, referenced by the await keyword, will have completed
But the await keyword is in the caller which is not statically known.
What is the difference between an async method and a method that returns a Future?