Home > Software engineering >  What is the diffrence between these definitions for `async inline methods`?
What is the diffrence between these definitions for `async inline methods`?

Time:07-17

We are able to define an async inline method in different ways, Is there any difference in the result or under the hood?

1. 
Future<void> counter() async => await Future<void>.delayed(
      const Duration(seconds: 5),
      () => print('Result'),
    );

2.
Future<void> counter() async => Future<void>.delayed(
      const Duration(seconds: 5),
      () => print('Result'),
    );

3.
Future<void> counter() => Future<void>.delayed(
      const Duration(seconds: 5),
      () => print('Result'),
    );

CodePudding user response:

Timing, if anything.

The async => await version waits for the delayed future created by Future.delayed to complete before returning, then it completes the returned future. The async => version should do precisely the same, since the await is implicit in the return. The => version returns the future created by Future.delayed directly.

In either case, the returned future will be completed after the 5 second duration has passed. It may or may not go through an extra intermediate microtask.

  • Related