44 Working with Future: map and flatMap

 

After reading this lesson, you will be able to

  • Manipulate the result of an asynchronous computation using the map operation
  • Merge two nested asynchronous computations using the flatten method
  • Combine multiple asynchronous operations using the flatMap function

In the previous lesson, you learned the basics of expressing asynchronous computations using the type Future. In this lesson, you’ll learn how to use the methods map, flatten, and flatMap for an instance of Future. You’ll notice that they share many commonalities with the map, flatten, and flatMap methods you have mastered for other types. The map function allows you to transform the value that an asynchronous computation produces. The flatten method merges two nested instances of Future into one. The flatMap operation is the composition of the methods map and flatten, and it allows you to chain multiple asynchronous instances. In the capstone, you’ll need to coordinate several asynchronous calls to read or write questions to a database for your quiz application.

44.1 The map, flatten, and flatMap operations

44.1.1 The map function

44.1.2 The flatten function

44.1.3 The flatMap function

Summary

Answers to quick checks

sitemap