I'm learning about async
and await
in Dart. For the most part, it makes sense that it's a way to say that some function will take a while and that the program should run a particular function asynchronously.
What I'm not understanding is how that affects all the "preceding" functions that eventually lead to the given asynchronous function.
For example,
import 'dart:async';
Future<String> firstAsync() async {
await Future<String>.delayed(const Duration(seconds: 2));
return "First!";
}
String secondAsync() async {
var returnVal = await firstAsync();
return returnVal;
}
void main() {
var s = secondAsync();
if (s == "First!") {
///run some code...
}
print('done');
}
firstAsync
returns a future, so it makes sense that the function's return value isFuture<String>
. Then,secondAsync
uses this function. Does that mean thatsecondAsync
must also returnFuture<String>
?Does
main
need theasync
keyword and would I need to change its first line tovar s = await secondAsync();
?And if so, does that mean that
main
also needs to be marked as returningFuture<void>
?