First, Please correct me anything I inferred about asynchrounous programming in dart, and even what I summarized from the docs,
And please check if my idea about when Future objects are correct. I cannot really think of how to experimentally test my ideas.
whats in the docs
- asynchronous operation
- doesn't block other code until its done
- synchronous operation
- blocks other code until it's done
- asynchronous function
- has at least one asynchrnous operation
- synchronous function
- has only synchronous operation
awaitcan only be used on functions withasynckeywordasyncforces the return value be of typeFuture<T>
what I inferred from experimenting with dart LSP
- asynchrounous function
- is an asynchrounous operation when called in dart
- is only synchronous when called with await, blocking next lines of code.
- is an asynchrounous operation when called in dart
asyncforces the return value be of typeFuture<T>- with the exception of void
- it is asynchrounous operation anyways
- but
awaitcan only be called on the function if the return type isFuture<void>
- with the exception of void
- if a Future is explicitly returned with a
returnkeyword- necessary to have a return value of type
Future<T>in the function definition - doesn't necessarily mean the function has
asyncmark
- necessary to have a return value of type
- if a future is not returned but there exist an asynchrounous operation in the function body
- return value can be just void
- return value can be
Future<void>with async mark as well
My Theory on return timing of Future objects
- I have some ideas on when exactly a Future is returned, based on my understanding of
Futureobjects - But I can't write an apt code to test this ideas below
- assume fast operation takes one second
- assume slow operation takes 100 seconds
void foo1 (){
fastAsynchrounousOperation();
slowSynchronousOperation();
}
void foo2 (){
slowAsynchrounousOperation();
fastSynchronousOperation();
}
void foo3 (){
fastSynchronousOperation();
slowAsynchrounousOperation();
}
void foo4 (){
slowSynchronousOperation();
fastAsynchrounousOperation();
}
Future<void> foo1 () async{
fastAsynchrounousOperation();
slowSynchronousOperation();
}
Future<void> foo2 () async{
slowAsynchrounousOperation();
fastSynchronousOperation();
}
Future<void> foo3 () async{
fastSynchronousOperation();
slowAsynchrounousOperation();
}
Future<void> foo4 () async{
slowSynchronousOperation();
fastAsynchrounousOperation();
}
- the above functions are valid, and when called, becomes asynchrounous operation. doesn't block anything.
Future<String> foo5 () async {
fastAsynchrounousOperation();
return slowSynchronousOperation();
}
- A completed future is returned only after 100 seconds. Uncompleted Future cannot be returned
- In other words, Future object either uncompleted/completed is only returned after the synchronous part of a function body is finished.
Future<String> foo6 () async {
slowAsynchrounousOperation();
return fastSynchronousOperation();
}
- A uncompleted future is returned after after 1 second(not right after the execution)
- future gets complete after 100 seconds
Future<String> foo7() async {
fastSynchrounousOperation();
return slowAsynchronousOperation();
}
- A uncompleted future is returned after 1 second
- future gets coplete after 101 seconds
Future<String> foo8 () async {
slowSynchrounousOperation();
return fastAsynchronousOperation();
}
- A uncompleted future is returned after 100 second
- future gets complete after 101 seconds
awaitis encountered, which means that if you have anasyncfunction that neverawaits anything, it's effectively a synchronous function. This means that in all of your example functions, all of them will "return" uncompleted futures and then complete synchronously with the futures not completing until the event thread is no longer blocked by the synchronous tasks.Future.delayedto experiment and validate your assumptions.