【问题标题】:About Dart async/await.I used a piece of code to test,But the print result is not as expected关于 Dart async/await。我用一段代码测试了一下,但是打印的结果并不如预期
【发布时间】:2019-12-18 09:39:57
【问题描述】:
void main() {
  methodA();
  methodB();
  methodC('main');
  methodD();
}

methodA(){
  print('A');
}

methodB() async {
  print('B start');
  await methodC('B');
  print('B end');
}

methodC(String from) async {
  print('C start from $from');
  Future((){                
    print('C running Future from $from');
  }).then((_){
    print('C end of Future from $from');
  });
  print('C end from $from');
}

methodD(){
  print('D');
}

打印结果如下:

A
B start
C start from B
C end from B
C start from main
C end from main
D
B end
C running Future from B
C end of Future from B
C running Future from main
C end of Future from main

我不明白为什么C start from mainC end from mainB end 之前打印?? 这就是为什么print('B end')还没有被执行,methodC('main')已经被执行了。 我以为我理解async/await,但直到我遇到这段代码。我希望得到帮助

【问题讨论】:

    标签: asynchronous flutter dart


    【解决方案1】:

    会发生什么:

    • 运行时系统调用main
    • main 致电methodA
    • methodA 打印 "A"
    • methodA 返回。
    • main 致电methodB
    • methodB 打印 “B 开始”
    • methodB 致电methodC("B")
    • methodC 打印 “C 从 B 开始”
    • methodC 使用 Future 构造函数创建一个未来,f1。这会安排一个 timer t1
    • methodCf1 上调用 then,这会在 f1 上添加一个监听器并返回一个未来 g1。 (这个未来被忽略了)。
    • methodC 打印 "C end from B"
    • methodC,因为它是 async,返回一个未来,h1,并安排一个微任务 m1 来用null 完成那个未来。
    • methodB 等待 h1
    • methodB,因为它是异步的,所以返回一个未来 k。 (这个未来被忽略了)。
    • main 致电methodC("main")
    • methodC 打印 "C start from main"
    • methodC 使用 Future 构造函数创建一个未来 f2。这会安排一个 timer t2
    • methodCf2 上调用 then,这会在 f2 上添加一个监听器并返回一个未来 g2。这个未来被忽略了。
    • methodC 打印 "C end from main"
    • methodC,因为它是 async,返回一个未来,h2,并安排一个微任务 m2 来用null 完成那个未来。 (未来被忽略)。
    • main 致电methodD
    • methodD 打印 "D"
    • methodD 返回。
    • main 返回。
    • 微任务循环运行微任务m1,它以空值完成未来h1
    • methodB 完成了它的 await 表达式,因为 h1 已经完成。
    • methodB 打印 “B 端”
    • methodBnull 完成未来 k。没人在听。
    • 微任务循环运行微任务 m2,它以 null 完成未来的 h2。没人在听。
    • 微任务队列现在为空。
    • 事件循环运行计时器t1,它
      • 打印“C running Future from B”
      • null完成f1的未来。
      • f1 上调用 then 侦听器和。
      • 打印"C end of Future from B"
      • 用 `null 完成 g1 未来。没有人在听。
    • 事件循环运行计时器t2,它
      • 打印“C running Future from main”
      • null完成f1的未来。
      • f1 上调用 then 侦听器和。
      • 打印“C end of Future from main”
      • 用 `null 完成 g1 未来。没有人在听。
    • 事件和微任务队列现在为空,程序退出。

    【讨论】:

    • 你真好!你真好!非常感谢非常感谢!
    【解决方案2】:

    当您使用 await 关键字时,在执行当前子例程/语句流之前,不会执行进一步的语句。

    例如:

    methodB() async {
      print('B start');
      await methodC('B');
      print('B end');
    }
    

    这里,在 print('B start') 之后,您使用 await 关键字调用了 methodC('B')。因此,现在 print('B end') 将在 methodC('B') 的所有语句和方法执行完毕后执行。到那时 print('B end') 语句将等待 methodC('B')

    的执行

    【讨论】:

    • 我明白你在说什么。methodB()main() 中位于methodC('main') 之前。为什么print('B end')还没有被执行,methodC('main')已经被执行了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多