An asynchronous future class, written in CoffeeScript.
An asynchronous Future class, written in CoffeeScript.
Futures provide a simple API for running parallel operations in the background, and pick up their results at a later time (in the "future").
By encapsulating the management of parallel operations, Futures allow to create sophisticated parallel application behavior using a simple, single-threaded programming style. This library provides Futures asynchronous interface, as is typical in Node.js environments. Futures support Node's asynchronous, event-driven flow patterns very naturally.
Here is an example:
- Start a parallel operation that performs a long-running method, store it in a Future.
- Do other things in your main thread. Take your time.
- When done with (2), pick up the result of the parallel operation from the future created in (1).
- Both long-running operations - (1) and (2) - have run in parallel, but were coded like a single-threaded application.
npm install async_future.coffee
user_future = load_useruser_id# Do something else now.user_futuregetconsolelog "This user was loaded in the background, and it didn't hurt: "
The directory /examples contains a number of usage examples that visualize better how futures work.
You can run them simply by doing
coffee [filename] in the terminal.
The directory /examples/delay_future demonstrates how futures operate on the time line.
1_waits_for_results.coffee: Demonstrates how Futures wait until they receive the results before they call the callback method given.
2_has_results.coffee: Demonstrates how Futures hold on to the result of the parallel operation until it is requested by their get method.
To run the tests:
$ npm test