import 'dart:async';
void main() {
print('main #1 of 2');
scheduleMicrotask(() => print('microtask #1 of 2'));
new Future.delayed(new Duration(seconds:1),
() => print('future #1 (delayed)'));
new Future(() => print('future #2 of 3'));
new Future(() => print('future #3 of 3'));
scheduleMicrotask(() => print('microtask #2 of 2'));
print('main #2 of 2');
}
输出
main #1 of 2
main #2 of 2
microtask #1 of 2
microtask #2 of 2
future #2 of 3
future #3 of 3
future #1 (delayed)
main先执行,Microtask队列执行,队列空,event队列执行,延时最后执行。
await可以变异步为同步,但是需要一层一层递进。
回调可以同步,但是需要暴露回调口。
项目中没用到Microtask,和provider,估计是研发不喜欢用或者他不会用。建议灵活使用。