Reputation: 14304
The tutorial on async programming here talks about async
await
, but avoids discussing the Future
API. I could find more information about the Future
API here, but still I have some questions. These tutorials made me raise some questions, ideally I should have placed one for each questions, but since they are small and related I preferred to ask them all in one place.
What triggers/starts a Future
execution?
From the text I can only conclude that once an async
method returns the Future
will be immediately triggered by the runtime.
What's the difference between Future.wait()
and Future.then().then().then()
?
Are return await myFuture;
and return myFuture
the same?
The text says an async
method will return an incomplete Future
once it sees an await
or a return
.
The text says:
Important: Async functions return Futures. If you don’t want your function to return a future, then use a different solution. For example, you might call an async function from your function.
How can we call an async
function, get its return value, and not await
, thus, not be an async
function?
Upvotes: 3
Views: 634
Reputation: 538
What triggers/starts a Future execution?
Future
is placed into the event queue, so it's executed when its turn in the event queue comes up. The Flutter in Focus Futures video has some good visuals about how this works.What's the difference between Future.wait() and Future.then().then().then()?
Future.wait
handles errors slightly differently, and its values are returned as a list rather than in sequential code blocks, but in practice, the difference might not matter for your situation.Are
return await myFuture;
andreturn myFuture
the same?
No. In the first instance, execution is paused at the await until the Future
is processed in the event queue. In the second instance, the Future
itself is returned to the caller and execution continues as the caller wishes (probably until the Future
is given a chance to be handled).
await
is a language feature the essentially waits on the Future to complete at that point, whereas return
simply returns the Future
itself to the caller.
How can we call an
async
function, get its return value, and notawait
, thus, not be anasync
function?
async
function and then use it's Future
directly rather than use await
. Here's a silly example:Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt().then((value) {
print("Got an async int: $value");
});
}
In the above example, you can see we use an async function, but testAsync
does not await the value (rather uses a Future, so the end result is the same).
If you don't need the return value though, you can just call the async
function directly though:
Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt();
}
In this second case, in fact, getAsyncInt()
will indeed be called, even though it's return value is ignored by the caller.
These are good questions, BTW, hope that helps. async/await
can be rather mysterious, but thinking of them as event queues really helps to understand in the flow of execution IMHO.
Upvotes: 2