I think I understand the idea behind async, returning a Future, but I am not clear on how async behaves on a very basic level. From my understanding, it does not automatically create asynchronous behavior in a program. For example:
import 'dart:async';
main() {
a();
b();
}
a() {
new Timer(new Duration(milliseconds: 20), () {}); // create latency
print("a");
}
b() {
print("b");
}
// a
// b
If you put async after the a(), the b() is executed first asynchronously, and a() executes after the given latency:
import 'dart:async';
main() {
a();
b();
}
a() **async** {
new Timer(new Duration(milliseconds: 20), () {}); // create latency
print("a");
}
b() {
print("b");
}
// b
// a
But if you put async after both a() and b(), a() executes first, similar to not using async at all:
import 'dart:async';
main() {
a();
b();
}
a() **async** {
new Timer(new Duration(milliseconds: 20), () {}); // create latency
print("a");
}
b() **async** {
print("b");
}
//a
//b
Is using async on all functions cancelling the async function completely?
Now, I think that the main() async does not actually activate the asynchronous behavior by itself. If you add async after the main(), nothing changes. However, it allows for you to use await, in case you must wait for the a() function to finish first, before you continue the program. Is this right?
import 'dart:async';
main() **async** {
**await** a();
b();
}
a() **async** {
new Timer(new Duration(milliseconds: 20), () {}); // create latency
print("a");
}
b() {
print("b");
}
// waits for the value of a() if you put **await** keyword
// a
// b
However, I see main() async {} alot as well as using it after the script html tag, yet there is no await anywhere. Does it mean different things in different contexts? I hope I have explained the logic adequately. Can you explain how I am misunderstanding the use of async/await? Thank you.