dart – 事件队列和微任务队列
在Dart文档
The Event Loop and Dart (2013)中,它提到任何Future都被添加到Event队列中.
它还提到Microtask队列总是首先运行,然后是事件队列. 这个文档很旧,似乎是面向Web开发的,所以我不确定Flutter是否与我执行此代码时不同. Future<String> myFunction() => new Future.value('Hello'); Future<String> myFunction2() => new Future.value('Hello2'); Future<void> mainTest() async { debugPrint("Sync1"); myFunction().then(debugPrint); scheduleMicrotask(() { debugPrint("Microtask"); }); myFunction2().then(debugPrint); debugPrint("Sync2"); } 我得到了一个输出 I/flutter ( 6731): Sync1 I/flutter ( 6731): Sync2 I/flutter ( 6731): Hello I/flutter ( 6731): Microtask I/flutter ( 6731): Hello2 但是如果所有Microtasks都要在下一个Event循环之前运行,那不应该这样吗? I/flutter ( 6731): Sync1 I/flutter ( 6731): Sync2 I/flutter ( 6731): Microtask // This running first before the Futures? I/flutter ( 6731): Hello I/flutter ( 6731): Hello2 解决方法
如果你在没有调用.then的情况下调用方法就是这种情况
所以当你调用myFunction().然后(打印);将未来添加到微任务队列中. 在没有’.then’的情况下拨打电话时的一些奖励事实:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |