//dark 事件循环
void main() {
// test0();
// test1();
// future3();
// future4();
// future5();
// future6();
// feture7();
// feture8();
feture9();
}
void feture9() { // 9->8->0->2->1->3->4->6->7->5 ( Future(() =>print('5')); 最后加入队列)
Future x = Future(() => null);
x.then((value){
print('0');
scheduleMicrotask(() => print('1'));
}).then((value)=>print('2'));
Future x1= Future(() => print('3'));
x1.then((value){
print('4');
Future(() =>print('5'));
}).then((value) => print('6'));
Future(() => print("7"));
scheduleMicrotask(() {
print('8');
});
print('9');
}
void feture8() { // 6->5->1->3->2->4 ( then((value)=>print('8')); 是先放入微任务中)
Future x = Future(() => null);
x.then((value){
print('1');
scheduleMicrotask(() => print('2'));
}).then((value)=>print('3'));
Future(() => print("4"));
scheduleMicrotask(() {
print('5');
});
print('6');
}
void feture7() { // 5->3->1->4->2
Future x = Future(() => print("1"));
Future(() => print("2"));
scheduleMicrotask(() {
print('3');
});
x.then((value) => print('4'));
print('5');
}
void future6() {
//beging -> end -> micotask->A /A over ->B /B over
print('beging');
Future(() => print("A")).then((value) => print("A over"));
Future(() => print("B")).then((value) => print("B over"));
scheduleMicrotask(() {
//priority height
print("micotask");
});
print('end');
}
void future5() {
//quque finish => print (task 1->task 2->task 3)
Future.wait([
Future(() {
return "task 1";
}),
Future(() {
return "task 2";
}),
Future(() {
return "task 3";
}),
]).then((value) => print(value[0] + value[1] + value[2]));
}
void future4() {
// queue 依赖 (task1 over-> task1 task2 over -> task1 task2 task3 over)
Future(() {
return "task1";
}).then((value) {
print("$value over");
return '$value task2';
}).then((value) {
print("$value over");
return '$value task3';
}).then((value) {
print("$value over");
return '$value task4';
});
}
void future3() async {
// queue 安照顺序执行 (task 1->task 2->task 3->task 4)
Future(() {
return "task 1";
}).then((value) => print('$value'));
Future(() {
sleep(Duration(seconds: 1));
return "task 2";
}).then((value) => print('$value'));
Future(() {
return "task 3";
}).then((value) => print('$value'));
Future(() {
return "task 4";
}).then((value) => print('$value'));
}
void test1() {
future1();
print('A'); // B->A->C->D
}
void future1() {
Future(() {
sleep(Duration(seconds: 1));
print('C');
}).then((value) => print('D'));
print('B');
}
void test2() {
future2();
print('A'); //A->C->D-> B
}
void future2() async {
// 整个函数都变成异步
await Future(() {
sleep(Duration(seconds: 1));
print('C');
}).then((value) => print('D'));
print('B');
}
void test0() {
future0();
print('做其他事情');
}
future0() async {
print('开始data=$_data');
//耗时操作
Future future = Future(() {
for (int i = 0; i < 1000000000; i++) {}
return '网络数据';
});
future.then((value) => print('结果data=$value'));
print('再干点事情!');
}
Dark 事件循环测试
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 浏览器下的事件循环:记住2种任务队列:宏任务队列:setTimeout、setInterval、setImmedi...
- 什么是事件循环 Node采用的是单线程的处理机制(所有的I/O请求都采用非阻塞的工作方式),至少从Node.js开...
- 在进行数据绑定时,会遇到这种这种参数没有被传进去的情况,注释的哪一个console.log(i)输出的都是trs....
- 打脸了,这并不是闭包的问题,而是一个事件循环机制 以下为错误解释这是一个关于变量闭包的问题,以前做项目时遇到,那时...