Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 16

Futures, async, and await

Some things take time: downloading a file, reading a database, waiting for a timer. Dart handles this with a **Future** — a value that does not exist yet, but will later.
Level: GeneralLength: Mid2 min read

Read at another depth

Futures, async, and await

Waiting without freezing

Some things take time: downloading a file, reading a database, waiting for a timer. Dart handles this with a Future — a value that does not exist yet, but will later.

Think of a Future like a ticket at a restaurant. You order now; the meal arrives later. The ticket (Future) is the promise of the meal.

Code example in dart
Future<String> fetchMessage() async {
  await Future.delayed(Duration(seconds: 1));
  return 'done';
}

This function is marked async, which means it may pause and resume instead of blocking everything else.

await: pausing inside an async function

The keyword await means "pause here until the Future is finished, then continue." Only an async function can use await.

Code example in dart
Future<void> main() async {
  print('start');
  final message = await fetchMessage();
  print(message); // prints after about one second
}

Note that main is async too, because it awaits. A program's work happens on a single thread, but while awaiting, Dart is free to do other pending work — the program does not freeze.

Handling errors in async code

If the Future fails, it carries an error instead of a value. The familiar try-catch from synchronous code works with await:

Code example in dart
Future<void> main() async {
  try {
    final message = await fetchMessage();
    print(message);
  } catch (e) {
    print('failed: $e');
  }
}

Because await is a normal expression, you can wrap it in the same try-catch you use everywhere else. Async errors become ordinary exceptions.

Running work in parallel

Sometimes you don't want to wait for tasks one at a time. Use Future.wait to start several and await them all together:

Code example in dart
Future<String> task(String name, int ms) async {
  await Future.delayed(Duration(ms: ms));
  return '$name finished';
}

Future<void> main() async {
  final results = await Future.wait([task('a', 300), task('b', 200)]);
  print(results); // [a finished, b finished]
}

The total wait is about 300 milliseconds, not 500, because the tasks run at the same time.

Common mistakes

A few traps show up often:

  • Forgetting async: using await inside a function not marked async is a compile error.
  • Awaiting in a loop by accident: for (final url in urls) { await fetch(url); } waits for each one in turn. For parallel work, collect Futures in a list and use Future.wait.
  • Calling an async function without awaiting it: the work still starts, but you get a Future, not the result — and errors may go unnoticed.
  • Using async on main without Future<void>: prefer Future<void> main() async so the tooling knows the program ends when the async work does.
A timeline showing an async function starting, hitting await, yielding control, then resuming when the Future completes.
100%
How an async function pauses and resumes around an await
Show diagram source
sequenceDiagram
    participant M as main
    participant F as fetchMessage
    M->>F: call fetchMessage()
    F-->>M: returns Future immediately
    Note over F: waits 1 second
    M->>M: await (pause this function only)
    F-->>M: Future completes with 'done'
    M->>M: continue with message

Exercise

What is printed first in Future<void> main() async { print('a'); final x = await somethingSlow(); print('b'); }?

Show answer

'a'. The lines before the await run immediately; 'b' only prints after the Future completes.

Exercise

You need to load three files, and each takes one second. How do you do it in about one second instead of three?

Show answer

Start all three loads at once (collect the Futures in a list) and await them together with Future.wait([loadA(), loadB(), loadC()]).

Exercise

What happens if an awaited Future fails and you did not use try-catch?

Show answer

The async function stops with that error, and the returned Future completes with the error, which surfaces as an unhandled exception.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Futures, async, and await

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that