Dart Concurrency: Async, Await, Future Keywords | Asynchronous Programming

Vinit MepaniVinit Mepani
1 min read
  1. Async: Think of it as saying, "I'm going to do something that takes time, but I won't stop everything else." So, you're telling Dart, "While the bread is toasting, I can do other tasks."

  2. Await: It's like pausing to check if something is ready. In our sandwich example, it's like saying, "Wait until the bread is done toasting before moving on."

  3. Future: This is Dart's way of promising something. It's like telling Dart, "I'm working on getting something for you, and when it's ready, I'll let you know."

Let's Understand with one Example :

// Async function that returns a Future
Future<String> makeSandwich() async {
  print("Putting bread in toaster...");

  // Simulating toasting time
  await Future.delayed(Duration(seconds: 3));

  print("Toasted bread is ready!");
  return "Delicious sandwich";
}

void main() async {
  print("Starting sandwich making process...");

  // Await the Future to get the result
  String sandwich = await makeSandwich();

  print("Made a sandwich: $sandwich");
  print("Lunchtime is a success!");
}

Output:

0
Subscribe to my newsletter

Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinit Mepani
Vinit Mepani

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"