From the documentation for the Future.microtask
constructor, it says:
* Creates a future containing the result of calling [computation]
* asynchronously with [scheduleMicrotask].
and the documentation for the regular Future
constructor states:
* Creates a future containing the result of calling [computation]
* asynchronously with [Timer.run].
I am wondering, what kind of implications do they have on coding, and when should we use one or another?
Creates a future containing the result of calling computation asynchronously with scheduleMicrotask. If executing computation throws, the returned future is completed with the thrown error.
An object representing a delayed computation. A Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available. For example: Future<int> future = getFuture(); future.
Technically speaking: A Future represents a computation that doesn't complete immediately. Whereas a normal function returns the result, an asynchronous function returns a Future, which will eventually contain the result. The Future will tell you when the result is ready.
The microtask queue is to queue async execution but avoid returning to the main event loop before these microtasks are finished. You can ensure some related activities to be completed entirely even when executed async before other async tasks/events queued in the main queue are executed.
All microtasks are executed before any other Future
s/Timer
s.
This means that you will want to schedule a microtask when you want to complete a small computation asynchronously as soon as possible.
void main() {
Future(() => print('future 1'));
Future(() => print('future 2'));
// Microtasks will be executed before futures.
Future.microtask(() => print('microtask 1'));
Future.microtask(() => print('microtask 2'));
}
You can run this example on DartPad.
The event loop will simply pick up all microtasks in a FIFO fashion before other futures. A microtask queue is created when you schedule microtasks and that queue is executed before other futures (event queue).
There is an outdated archived article for The Event Loop and Dart, which covers the event queue and microtask queue here.
You can also learn more about microtasks with this helpful resource.
Here is a simple example of how code would run in sequence in terms of how Future
s are executed. In the example below, the resulting print statements wouldn't be in alphabetical order.
void main() async{
print("A");
await Future((){
print("B");
Future(()=>print("C"));
Future.microtask(()=>print("D"));
Future(()=>print("E"));
print("F");
});
print("G");
}
The resulting print statements would end up in the order shown below. Notice how B, F, and G gets printed first, then C, then E. This is because B,F, and G are synchronous. D then gets called before C and E because of it being a microtask.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With