@@ -41,8 +41,7 @@ environment to resize itself if necessary to guarantee progress.
4141A ` Future ` is an object holding a value which may become available at some point.
4242This value is usually the result of some other computation.
4343Since this computation may fail with an exception, the ` Future `
44- either holds a result of the computation or an exception in case
45- the computation failed.
44+ may also hold an exception in case the computation throws one.
4645Whenever a ` Future ` gets either a value or an exception, we say
4746that the ` Future ` is ** completed** .
4847When a ` Future ` is completed with a value, we say that the future
@@ -53,23 +52,23 @@ When a `Future` is completed with an exception, we say that the
5352A ` Future ` has an important property that it may only be assigned
5453once.
5554Once a ` Future ` object is given a value or an exception, it becomes
56- in effect immutable-- it can never be overwritten with a new value .
55+ in effect immutable-- it can never be overwritten.
5756
5857The simplest way to create a future object is to invoke the ` future `
5958method which starts an asynchronous computation and returns a
6059future holding the result of that computation.
6160The result becomes available once the future completes.
6261
63- Note that ` Future ` is a type which denotes future objects, whereas
62+ Note that ` Future[T] ` is a type which denotes future objects, whereas
6463` future ` is a method which creates and schedules an asynchronous
6564computation, and then returns a future object which will be completed
6665with the result of that computation.
6766
6867This is best shown through an example.
69- Let's assume that we want to use the hypothetical API of some
68+ Let's assume that we want to use a hypothetical API of some
7069popular social network to obtain a list of friends for a given user.
71- We will open a new session used to send requests and then send
72- the request to obtain a list of friends of a particular user:
70+ We will open a new session and then send
71+ a request to obtain a list of friends of a particular user:
7372
7473import scala.concurrent._
7574import ExecutionContext.Implicits.global
@@ -100,7 +99,7 @@ responds.
10099
101100An unsuccessful attempt may result in an exception. In
102101the following example, the ` session ` value is incorrectly
103- initialized, so the computation in the ` future ` block will yield a ` NullPointerException ` .
102+ initialized, so the computation in the ` future ` block will throw a ` NullPointerException ` .
104103This future ` f ` is then failed with this exception instead of being completed successfully:
105104
106105 val session = null
@@ -110,12 +109,12 @@ This future `f` is then failed with this exception instead of being completed su
110109
111110The line ` import ExecutionContext.Implicits.global ` above imports
112111the default global execution context.
113- Execution contexts execute tasks submitted to them asynchronously.
114- You can think of execution contexts as thread pools.
112+ Execution contexts execute tasks submitted to them, and
113+ you can think of execution contexts as thread pools.
115114They are essential for the ` future ` method because
116115they handle how and when the asynchronous computation is executed.
117- You can define your own execution contexts and use them with ` future `
118- as will be shown later, but for now it is sufficient to know that
116+ You can define your own execution contexts and use them with ` future ` ,
117+ but for now it is sufficient to know that
119118you can import the default execution context as shown above.
120119
121120Our example was based on a hypothetical social network API where
@@ -174,7 +173,7 @@ value is a `Throwable`.
174173
175174Coming back to our social network example, let's assume we want to
176175fetch a list of our own recent posts and render them to the screen.
177- We do so by calling a hypothetical method ` getRecentPosts ` which returns
176+ We do so by calling a method ` getRecentPosts ` which returns
178177a ` List[String] ` -- a list of recent textual posts:
179178
180179val f: Future[List[String]] = future{
@@ -250,7 +249,7 @@ of the keyword to the screen:
250249The ` onComplete ` , ` onSuccess ` , and
251250` onFailure ` methods have result type ` Unit ` , which means invocations
252251of these methods cannot be chained. Note that this design is intentional,
253- to prevent suggesting that chained
252+ to avoid suggesting that chained
254253invocations may imply an ordering on the execution of the registered
255254callbacks (callbacks registered on the same future are unordered).
256255
@@ -330,8 +329,8 @@ thus being eligible for GC.
330329
331330The callback mechanism we have shown is sufficient to chain future
332331results with subsequent computations.
333- However, it is sometimes inconvenient as it may become bulky.
334- We show this through an example. Assume we have an API for
332+ However, it is sometimes inconvenient and results in bulky code .
333+ We show this with an example. Assume we have an API for
335334interfacing with a currency trading service. Suppose we want to buy US
336335dollars, but only when it's profitable. We first show how this could
337336be done using callbacks:
@@ -379,8 +378,8 @@ more straightforward composition. One of the basic combinators
379378is ` map ` , which, given a future and a mapping function for the value of
380379the future, produces a new future that is completed with the
381380mapped value once the original future is successfully completed.
382- You can reason about ` map ` ping futures as you reason about ` map ` ping
383- collections.
381+ You can reason about ` map ` ping futures in the same way you reason
382+ about ` map ` ping collections.
384383
385384Let's rewrite the previous example using the ` map ` combinator:
386385
@@ -400,7 +399,7 @@ Let's rewrite the previous example using the `map` combinator:
400399By using ` map ` on ` rateQuote ` we have eliminated one ` onSuccess ` callback and,
401400more importantly, the nesting.
402401If we now decide to sell some other currency, it suffices to use
403- ` map ` on ` purchase ` .
402+ ` map ` on ` purchase ` again .
404403
405404But what happens if ` isProfitable ` returns ` false ` , hence causing
406405an exception to be thrown?
@@ -410,7 +409,7 @@ Furthermore, imagine that the connection was broken and that
410409In that case we'd have no value to map, so the ` purchase ` would
411410automatically be failed with the same exception as ` rateQuote ` .
412411
413- More formally , if the original future is
412+ In conclusion , if the original future is
414413completed successfully then the returned future is completed with a
415414mapped value from the original future. If the mapping function throws
416415an exception the future is completed with that exception. If the
@@ -457,18 +456,19 @@ The for-comprehension above is translated into:
457456}
458457
459458which is a bit harder to grasp than the for-comprehension, but
460- we analyze to better understand the ` flatMap ` operation.
459+ we analyze it to better understand the ` flatMap ` operation.
461460The ` flatMap ` operation maps its own value into some other future.
462461Once this different future is completed, the resulting future
463462is completed with its value.
464- In our example, ` flatMap ` used the value of the ` usdQuote ` future
463+ In our example, ` flatMap ` uses the value of the ` usdQuote ` future
465464to map the value of the ` chfQuote ` into a third future which
466465sends a request to buy a certain amount of Swiss francs.
467- Only once this third future completes is the resulting future completed.
466+ The resulting future ` purchase ` is completed only once this third
467+ future returned from ` map ` completes.
468468
469469This can be mind-boggling, but fortunately the ` flatMap ` operation
470470is seldom used outside for-comprehensions, which are easier to
471- understand.
471+ use and understand.
472472
473473The ` filter ` combinator creates a new future which contains the value
474474of the original future only if it satisfies some predicate. Otherwise,
@@ -715,7 +715,7 @@ in which future the computation failed.
715715
716716## Promises
717717
718- So far we have only considered ` Future ` objects created through
718+ So far we have only considered ` Future ` objects created by
719719asynchronous computations started using the ` future ` method.
720720However, futures can also be created using * promises* .
721721
@@ -733,7 +733,7 @@ may be the case that `p.future eq p`.
733733
734734Consider the following producer-consumer example, in which one computation
735735produces a value and hands it off to another computation which consumes
736- that value. This passing of the value is done through a future .
736+ that value. This passing of the value is done using a promise .
737737
738738import scala.concurrent.{future, promise }
739739import scala.concurrent.ExecutionContext.Implicits.global
0 commit comments