简体   繁体   中英

In a Dart async method, can certain lines that don't share any dependencies be run asynchronously?

Suppose you have an async method body, like below, with a method call that returns a Future and a print following it that doesn't not need the result of that method. Is it possible to add some structure to get the print statement to execute independently? Or does this syntactic sugar force an async method body to wholly run sequentially?

Future<String> test2() {
  Completer c = new Completer();

  new Timer(new Duration(seconds: 2), () {
    c.complete('test2: done');
  });

  return c.future;
}

Future<String> test1() async {
  String result = await test2();
  print(result);

  // Why is this not executed immediately before test2 call completes?
  print('hello');

  return 'test1: done';
}

void main() {
  test1().then((result) => print(result));
}

Follow up: I've added below a rewrite of test1() which has chained async method calls. I'm really after how to use the async syntactic sugar to simplify this type of use case. How could I rewrite this block with the new syntax?

Future<String> test1() async {
  test2().then((result) {
    print(result);
    test2().then((result) {
      print(result);
    });
  });

  // This should be executed immediately before
  // the test2() chain completes.
  print('hello');

  return 'test1: done';
}

edit for follow up:

I am not sure what you want to do after all, but if you want wait for the chain to finish before printing things, here's what you have to do:

Future<String> test1() async {
  String result;
  var result1 = await test2();
  print("Printing result1 $result1 ; this will wait for the first call to test2() to finish to print");
  var result2 = await test2();
  print("Printing result2 $result2 ; this will wait for the second call to test2() to print");  

  // This should be executed immediately before
  // the test2() chain completes.
  print('hello');

  return 'test1: done';
}

Now if you call test1() and wait for it to return "test1: done" you have to await it.

main() async {
  var result = await test1();
  print(result); // should print "hello" and "test1: done"
}

If you want to execute code independently from a previous future result, just don't put the await keyword.

Future<String> test1() async {
  test2().then((String result) => print(result)); // the call to test2 will not stop the flow of test1(). Once test2's execution will be finished, it'll print the result asynchronously

  // This will be printed without having to wait for test2 to finish.
  print('hello');

  return 'test1: done';
}

The keyword await makes the flow stalled until test2() is finished.

This is because of the await before test2(); . The execution is stalled until the Future returned by test2() is completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM