Michel Feinstein
Michel Feinstein

Reputation: 14304

Questions about Future in Dart

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.

  1. 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.

  2. What's the difference between Future.wait() and Future.then().then().then()?

  3. 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.

  4. 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

Answers (1)

spenster
spenster

Reputation: 538

What triggers/starts a Future execution?

  1. The code block for a 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()?

  1. They are different ways of handling multiple futures, but the differences are subtle. 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; and return myFuture the same?

  1. 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 not await, thus, not be an async function?

  1. If you need the return value, then you'd call the 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

Related Questions