I'm new to Dart. As i understood from some articles and docs (for example this article):
async/await
uses the same mechanism asFuture
, and that is FIFOEvent Loop
.Event Loop
launches after the execution ofmain()
.async
functions runs synchronously up to the firstawait
keyword. Then they pause the execution, and execution returns to the previous function in call stack.- the remaining code is wrapped to the
Future
and queued to theEvent Loop
(most unsure point for me).
Based on these points i expect, that the following code:
main() {
Future(() => print("1"));
Future(() {
print("2");
asyncFunc("2");
});
Future(() => print("3"));
print("main");
asyncFunc("main");
print("\nEvent loop starts?\n");
}
asyncFunc(String from) async {
print("start [from $from]");
await (() async => print("middle [from $from]"))();
print("end [from $from]");
}
Will create something similar to this event queue after main() execution:
future1
-> future2
-> future3
-> end_from_main
And after execution of future2
, event end_from_future2
will be queued to the end:
future1
-> future2
-> future3
-> end_from_main
-> end_from_future2
So result output i expect should be:
main
start [from main]
middle [from main]
Event loop starts?
1
2
start [from 2]
middle [from 2]
3
end [from main]
end [from 2]
But in fact it returns:
main
start [from main]
middle [from main]
Event loop starts?
end [from main]
1
2
start [from 2]
middle [from 2]
end [from 2]
3
So the conclusion i made: Either async/await
events has priority over Fututre
. Either they use diffrent mechanism, unrelated to EventLoop
. Or maybe i misunderstand something hardly..