Skip to content

Commit c5748e5

Browse files
committed
Better future/promise documentation.
1 parent 6ff743f commit c5748e5

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

doc/future.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
`Future` is inspired by [Clojure's](http://clojure.org/) [future](http://clojuredocs.org/clojure_core/clojure.core/future) function. A future represents a promise to complete an action at some time in the future. The action is atomic and permanent. The idea behind a future is to send an operation for asynchronous completion, do other stuff, then return and retrieve the result of the async operation at a later time. `Future`s run on the global thread pool.
1+
`Future` is inspired by [Clojure's](http://clojure.org/) [future](http://clojuredocs.org/clojure_core/clojure.core/future) function. A future represents a promise to complete an action at some time in the future. The action is atomic and permanent. The idea behind a future is to send an operation for asynchronous completion, do other stuff, then return and retrieve the result of the async operation at a later time. `Future`s run on the global thread pool.
22

33
```cucumber
44
Feature:
@@ -7,19 +7,19 @@ Feature:
77
So I can perform other tasks without waiting
88
```
99

10-
`Future`s have four possible states: *:unscheduled*, *:pending*, *:rejected*, and *:fulfilled*. When a `Future` is created it is set to *:unscheduled*. Once the `#execute` method is called the state becomes *:pending* and will remain in that state until processing is complete. A completed `Future` is either *:rejected*, indicating that an exception was thrown during processing, or *:fulfilled*, indicating success. If a `Future` is *:fulfilled* its `#value` will be updated to reflect the result of the operation. If *:rejected* the `reason` will be updated with a reference to the thrown exception. The predicate methods `#unscheduled?`, `#pending?`, `#rejected?`, and `#fulfilled?` can be called at any time to obtain the state of the `Future`, as can the `#state` method, which returns a symbol.
10+
`Future`s have several possible states: *:unscheduled*, *:pending*, *:processing*, *:rejected*, or *:fulfilled*. These are also aggregated as `#incomplete?` and `#complete?`. When a `Future` is created it is set to *:unscheduled*. Once the `#execute` method is called the state becomes *:pending*. Once a job is pulled from the thread pool's queue and is given to a thread for processing (often immediately upon `#post`) the state becomes *:processing*. The future will remain in this state until processing is complete. A future that is in the *:unscheduled*, *:pending*, or *:processing* is considered `#incomplete?`. A `#complete?` `Future` is either *:rejected*, indicating that an exception was thrown during processing, or *:fulfilled*, indicating success. If a `Future` is *:fulfilled* its `#value` will be updated to reflect the result of the operation. If *:rejected* the `reason` will be updated with a reference to the thrown exception. The predicate methods `#unscheduled?`, `#pending?`, `#rejected?`, and `#fulfilled?` can be called at any time to obtain the state of the `Future`, as can the `#state` method, which returns a symbol.
1111

1212
Retrieving the value of a `Future` is done through the `#value` (alias: `#deref`) method. Obtaining the value of a `Future` is a potentially blocking operation. When a `Future` is *:rejected* a call to `#value` will return `nil` immediately. When a `Future` is *:fulfilled* a call to `#value` will immediately return the current value. When a `Future` is *:pending* a call to `#value` will block until the `Future` is either *:rejected* or *:fulfilled*. A *timeout* value can be passed to `#value` to limit how long the call will block. If `nil` the call will block indefinitely. If `0` the call will not block. Any other integer or float value will indicate the maximum number of seconds to block.
1313

14-
The constructor can also be given zero or more processing options. Currently the only supported options are those recognized by the [Dereferenceable](Dereferenceable) module.
14+
The constructor can also be given zero or more processing options. Currently the only supported options are those recognized by the [Dereferenceable](Dereferenceable) module.
1515

16-
The `Future` class also includes the behavior of the Ruby standard library [Observable](http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html) module, but does so in a thread-safe way. On fulfillment or rejection all observers will be notified according to the normal `Observable` behavior. The observer callback function will be called with three parameters: the `Time` of fulfillment/rejection, the final `value`, and the final `reason`. Observers added after fulfillment/rejection will still be notified as normal. The notification will occur on the global thread pool.
16+
The `Future` class also includes the behavior of the Ruby standard library [Observable](http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html) module, but does so in a thread-safe way. On fulfillment or rejection all observers will be notified according to the normal `Observable` behavior. The observer callback function will be called with three parameters: the `Time` of fulfillment/rejection, the final `value`, and the final `reason`. Observers added after fulfillment/rejection will still be notified as normal. The notification will occur on the global thread pool.
1717

1818
### Examples
1919

2020
A fulfilled example:
2121

22-
```ruby
22+
```ruby
2323
require 'concurrent'
2424
require 'thread' # for Queue
2525
require 'open-uri' # for open(uri)
@@ -57,7 +57,7 @@ count.pending? #=> true
5757

5858
count.value #=> nil (after blocking)
5959
count.rejected? #=> true
60-
count.reason #=> #<StandardError: Boom!>
60+
count.reason #=> #<StandardError: Boom!>
6161
```
6262

6363

lib/concurrent/promise.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ module Concurrent
2828
# When a promise is rejected all its children will be summarily rejected and
2929
# will receive the reason.
3030
#
31-
# Promises have four possible states: *unscheduled*, *pending*, *rejected*,
32-
# and *fulfilled*. A Promise created using `.new` will be *unscheduled*. It is
33-
# scheduled by calling the `execute` method. Upon execution the Promise and
34-
# all its children will be set to *pending*. When a promise is *pending* it
35-
# will remain in that state until processing is complete. A completed Promise
36-
# is either *rejected*, indicating that an exception was thrown during
37-
# processing, or *fulfilled*, indicating it succeeded. If a Promise is
38-
# *fulfilled* its `value` will be updated to reflect the result of the
39-
# operation. If *rejected* the `reason` will be updated with a reference to
40-
# the thrown exception. The predicate methods `unscheduled?`, `pending?`,
41-
# `rejected?`, and `fulfilled?` can be called at any time to obtain the state
42-
# of the Promise, as can the `state` method, which returns a symbol. A Promise
43-
# created using `.execute` will be *pending*, a Promise created using
44-
# `.fulfill(value)` will be *fulfilled* with the given value and a Promise
45-
# created using `.reject(reason)` will be *rejected* with the given reason.
31+
# Promises have several possible states: *:unscheduled*, *:pending*,
32+
# *:processing*, *:rejected*, or *:fulfilled*. These are also aggregated as
33+
# `#incomplete?` and `#complete?`. When a Promise is created it is set to
34+
# *:unscheduled*. Once the `#execute` method is called the state becomes
35+
# *:pending*. Once a job is pulled from the thread pool's queue and is given
36+
# to a thread for processing (often immediately upon `#post`) the state
37+
# becomes *:processing*. The future will remain in this state until processing
38+
# is complete. A future that is in the *:unscheduled*, *:pending*, or
39+
# *:processing* is considered `#incomplete?`. A `#complete?` Promise is either
40+
# *:rejected*, indicating that an exception was thrown during processing, or
41+
# *:fulfilled*, indicating success. If a Promise is *:fulfilled* its `#value`
42+
# will be updated to reflect the result of the operation. If *:rejected* the
43+
# `reason` will be updated with a reference to the thrown exception. The
44+
# predicate methods `#unscheduled?`, `#pending?`, `#rejected?`, and
45+
# `#fulfilled?` can be called at any time to obtain the state of the Promise,
46+
# as can the `#state` method, which returns a symbol.
4647
#
4748
# Retrieving the value of a promise is done through the `value` (alias:
4849
# `deref`) method. Obtaining the value of a promise is a potentially blocking

0 commit comments

Comments
 (0)