/event_loop.dart Secret
Created
June 9, 2022 12:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
/* | |
* @description: event loop | |
* | |
* @author: jixiaoyong | |
* @email: jixiaoyong1995@gmail.com | |
* @date: 6/9/2022 | |
*/ | |
main() { | |
scheduleMicroTaskFunc(); | |
futureFunc(); | |
timerFunc(); | |
} | |
void scheduleMicroTaskFunc() { | |
scheduleMicrotask(() { | |
print("Hello, I am microtask"); | |
}); | |
Future.microtask( | |
() => print("Hello, I am microtask created by Future.microtask")); | |
} | |
void futureFunc() async { | |
Future.delayed(const Duration(seconds: 1), () { | |
print("Hello, I am future"); | |
}); | |
Future.value(1).then((value) { | |
print("Hello, I am future created by Future.value"); | |
}); | |
Future.sync(() => print("Hello, I am future created by Future.sync")); | |
Future.forEach( | |
[1, 2, 3], | |
(element) => | |
print("Hello, I am future($element) created by Future.forEach")); | |
Future.error(Exception("Hello, I am future created by Future.error")) | |
.onError((error, stackTrace) => print(error)); | |
Future.any([ | |
Future(() { | |
return "I am Future run immediately Future.any"; | |
}), | |
Future.delayed(const Duration(seconds: 1), () { | |
return "I am Future run delay, will be discard Future.any"; | |
}) | |
]).then((value) => print(value)); | |
Future.wait([ | |
Future(() { | |
return "I am Future run immediately Future.wait 1/2"; | |
}), | |
Future.delayed(const Duration(seconds: 1), () { | |
return "I am Future run delay Future.wait 2/2"; | |
}) | |
]).then((value) => print(value)); | |
var repeatCounter = 0; | |
Future.doWhile(() async { | |
if (repeatCounter++ < 3) { | |
print("repeat ($repeatCounter/3) inner Future.doWhile"); | |
await Future.delayed(const Duration(seconds: 1)); | |
return true; | |
} | |
return false; | |
}); | |
} | |
void timerFunc() { | |
Timer.periodic(const Duration(seconds: 1), (timer) { | |
print("Hello, I am running inner(${timer.tick}/2) Timer.periodic"); | |
if (timer.tick == 2) { | |
timer.cancel(); | |
} | |
}); | |
Timer.run(() { | |
print( | |
"Hello, I will run asynchronously as soon as possible with Timer.run"); | |
}); | |
} | |
/// result: | |
/* | |
Hello, I am future created by Future.sync | |
Hello, I am future(1) created by Future.forEach | |
Hello, I am future(2) created by Future.forEach | |
Hello, I am future(3) created by Future.forEach | |
repeat (1/3) inner Future.doWhile | |
Hello, I am microtask | |
Hello, I am microtask created by Future.microtask | |
Hello, I am future created by Future.value | |
Exception: Hello, I am future created by Future.error | |
I am Future run immediately Future.any | |
Hello, I will run asynchronously as soon as possible with Timer.run | |
Hello, I am future | |
[I am Future run immediately Future.wait 1/2, I am Future run delay Future.wait 2/2] | |
repeat (2/3) inner Future.doWhile | |
Hello, I am running inner(1/2) Timer.periodic | |
Hello, I am running inner(2/2) Timer.periodic | |
repeat (3/3) inner Future.doWhile | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment