Reputation: 2911
I'm facing a problem with async
methods and Future
in Dart.
I think I did/understood something wrong but I don't know what.
I'm trying to figure out the difference between Future
and async
and understand how the event loop works. I read the documentation and a lot of articles about that. I thought I understood so I tried to write some code that create a Future
object with a sleep()
call in it.
First, I tried with Future
and I think it's behaving like it should:
main(List<String> arguments) {
print('before future');
test_future();
print('after future');
}
test_future() {
Future (() {
print('Future active before 5 seconds call');
sleep(Duration(seconds: 5));
print('Future active after 5 seconds call');
}).then((_) => print("Future completed"));
}
So this returns:
I think all of this is normal.
Now, i'm trying to do the same with async
. From the documentation, adding the async
keyword to a function make it return a Future
immediately.
So I wrote this:
main(List<String> arguments) {
print('before future 2');
test().then((_) => print("Future completed 2"));
print('after future 2');
}
test() async {
print('Future active before 5 seconds call');
sleep(Duration(seconds: 5));
print('Future active after 5 seconds call');
}
Normally, when calling test().then()
, it should put the content of test()
in the event queue and return a Future
immediately but no. The behavior is this one:
Can someone explain if I did not use async
properly or if there is something wrong ?
Best
Upvotes: 2
Views: 108
Reputation: 658205
You should be aware that sleep() just blocks the whole program. sleep() is not related in any way to the event loop or async execution. Perhaps you want to use instead:
await Future.delayed(const Duration(seconds: 5), (){});
async system calls do not block the isolate. The event queue is still processed, (continues immediately after invoking the system call). If you make sync system calls, they block like sleep.
There are often sync and async variants in dart:io
of system calls like api.dartlang.org/stable/2.2.0/dart-io/File/readAsLinesSync.html. Even though sleep
does not have the sync suffix, it's sync and no way to work around. You can use Future.delayed()
as shown above to get the effect in an async way.
Upvotes: 3