Home > Software engineering >  What happens exactly when I call an async method?
What happens exactly when I call an async method?

Time:09-17

What if I have

_asyncMethod();

print("Hi");

The method is executed before print but the method stops at its first await and then print is reached?

OR

The method is NOT executed, the print is reached and then the method is executed, like it was called with a Future.delayed with Duration(seconds:0) call?

CodePudding user response:

_asyncMethod() is executed. As much of it as possible is executed synchronously. That is, _asyncMethod is executed until it reaches its first await (or return statement), which is syntactic sugar for returning a Future to the caller. Since the caller does not itself await the returned Future, the caller resumes executing, which in this case will call print("Hi").

  •  Tags:  
  • dart
  • Related