Capture and Return Asynchronous Values with Futures in Dart

InstructorJermaine Oppong

Share this video with your friends

Send Tweet

Futures represent a computation that does not complete immediately. Where a normal function returns a result, an asynchronous function returns a Future that will eventually contain the result. These are similar to Promises in JavaScript. In this lesson, we will learn how to program asynchronously by writing logic to capture values that are returned at a later time.

Hosarsiph
~ 5 years ago

Thanks for the explanation Jermaine Oppong. One question, what would it be same as Promise.all with Futures in dart lang?

Hosarsiph
~ 5 years ago
Jermaine Opponginstructor
~ 5 years ago

Hey @Hosarsiph, glad I could help. The equivalent would be Future.wait():

var a = Future(() => 5);
var b = Future(() => 6);
var c = Future(() => 7);

void main() async {
  Future.wait([a, b, c])
    .then(print);
}

Learn more here: https://api.dartlang.org/stable/2.3.0/dart-async/Future/wait.html

You can try the snippet on DartPad

Hosarsiph
~ 5 years ago

Awesome! Thanks.