Eternalcode Eternalcode - 4 months ago 11
Dart Question

Can anyone explain this behavior?

I accidentally came across this behavior while playing around the async behavior.

https://dartpad.dartlang.org/6cac2e240375b531630c9e47e3875b63

Can anyone explain to me why the setTimeout with 0 seconds is not executing the print statement immediately?

Answer

setTimeout enqueues the passed function into the event queue and the passed code is executed async. https://webdev.dartlang.org/articles/performance/event-loop

The sync code is executed to the end and then the next event in the event queue is processed.