45 Working with Future: For-comprehension and other operations

 

After reading this lesson, you will be able to

  • Define a chain of asynchronous operations using for-comprehension
  • Select the first Future instance to complete, either successfully or not
  • Find the fastest asynchronous operation to produce a value with specific properties
  • Run independent Future instances in parallel and collect their results in a sequence

You discovered how you can use the map, flatten, and flatMap operations to manipulate the value that asynchronous computations produce. In this lesson, you’ll learn how to coordinate multiple Future instances using for-comprehension. In particular, you’ll see how to provide requirements on when each asynchronous computation should start. You’ll discover how to select the first Future instance to complete among many and find the one that successfully produces a value with given features. You’ll also see how to combine the results of several asynchronous independent computations running in parallel into one sequence. In the capstone, you’ll use for-comprehension to define asynchronous computations to execute in a given order for your quiz program.

45.1 For-comprehension

45.2 Retrieving the first Future to complete

Summary

Answers to quick checks