Home > Software design >  Is there a time lag on the object?
Is there a time lag on the object?

Time:01-09

The two functions are

Future<int> functinoA()  {
  return Future.delayed(const Duration(seconds: 3), () => 1);
}
Future<int> functionB() async {
  return await Future.delayed(const Duration(seconds: 3), () => 1);
}

Although it takes 3 seconds to create both object A created by function A and object B created by function B, is there a time lag in their subsequent calls?

My understanding is that object A is delayed by three seconds each time it is invoked because it is an object with a delay of 3 seconds property created at the time of creation.

On the other hand, object B is an object without delay property created by waiting 3 seconds during the creation process, so it can be invoked immediately each time it is invoked. This difference is due to the use of async and await.

If my understanding is incorrect, please tell me the difference between these two functions and the difference between using async and await.

In addition, if my understanding is correct, how do I write a self-adding function for the two objects so that the time difference of the objects can be clearly seen? I've tried several ways to see the time lag caused by the self-adding equation, but it's possible that the equation I wrote was too simple to see the effect because of the computing efficiency. Or maybe my understanding is wrong.

CodePudding user response:

It seems to me you're reading too much into what aync/await do. They're just a better syntax for doing asynchronous work so you don't end up with a bunch of nested callbacks. They don't change anything about how objects are created & reused.

This is easy to test though. Try running this:

Future<int> functionA()  {
  return Future.delayed(const Duration(seconds: 3), () => 1);
}

Future<int> functionB() async {
  return await Future.delayed(const Duration(seconds: 3), () => 1);
}


Future measureTime(Function() callback) async {
  final start = DateTime.now();
  await callback();
  final end = DateTime.now();
  print("Duration: ${end.difference(start)}");
}

Future main() async {
  await measureTime(functionA);
  await measureTime(functionA);
  await measureTime(functionB);
  await measureTime(functionB);
}

which results in:

Duration: 0:00:03.004000
Duration: 0:00:03.005000
Duration: 0:00:03.006000
Duration: 0:00:03.001000

See this gist.

Now, to talk about what's happening.

Future<int> functionA()  {
  return Future.delayed(const Duration(seconds: 3), () => 1);
}

In function A, you're returning a Future, which is going to return 1 after a delay of 3 seconds. Every time you call functionA, it's creating a new Future, every time you wait for it to complete it will take 3 seconds.

Future<int> functionB() async {
  return await Future.delayed(const Duration(seconds: 3), () => 1);
}

In function b, what you're doing is actually very close to the same. You're still creating a new future which is going to wait 3 seconds before returning every time the function is called, but the await just means that it is going to wait until that Future is complete, then do a noop before returning from the function.

You can think of async/await as basically syntactic sugar on top of Futures, so if you 'unwound' it, function B would look like this:

Future<int> functionB() {
  return Future.delayed(const Duration(seconds: 3), () => 1)
    .then((result) => result);
}

When the functions are this short, there's not much difference. But if we took a look at a longer function it might be clearer why async/await is useful.

Future<int> functionC() async {
  final firstPart = await Future.delayed(const Duration(seconds: 3), () => 1);
  final secondPart = await Future.delayed(const Duration(seconds: 1), () => firstPart   1);
  return secondPart;
}

could be translated to:

Future<int> functionC() {
  return Future.delayed(const Duration(seconds: 3), () => 1)
    .then((firstPart) {
      return Future.delayed(const Duration(seconds: 1, () => firstPart   1);
    });
}

This second function is a lot harder to read, whereas the first can be read just like normal code. And you could actually even write it like this to make even more legible:

Future<int> functionC() async {
  await Future.delayed(const Duration(seconds: 3);
  final firstPart = 1;
  await Future.delayed(const Duration(seconds: 1));
  return firstPart   1;
}
  • Related