Concurrent
Promises/A+ with Scala Awesomeness
Examples
var Future = Future;var request = ; var { var future = ; ; return future;}; /** * Example #1: Simple Async Call */// Fetch Google with a SLAvar google = ; // Fetch the statusvar status = google; // Log the final resultstatus; /** * Examples #2: Parallel Calls */var duckDuckGo = ;var bingOrYahoo = ; var asArray = Future; asArray; var asObject = Future; var recoverable = asObject;
Benchmarks
Here are the top 5 libraries for some of the tests
==========================================================Test: promise-fulfill x 10000----------------------------------------------------------laissez: | 3.00concurrent: ▇▇ 14.00deferred: ▇▇ 19.00when: ▇▇▇▇▇ 36.00q: ▇▇▇▇▇▇▇▇ 57.00 ==========================================================Test: promise-reject x 10000----------------------------------------------------------concurrent: | 6.00avow: ▇▇▇▇▇▇▇▇ 55.00q: ▇▇▇▇▇▇▇▇▇ 60.00when: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 89.00laissez: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 118.00 ==========================================================Test: promise-sequence x 10000----------------------------------------------------------laissez: | 8.00when: ▇▇▇▇▇▇▇▇ 204.00concurrent: ▇▇▇▇▇▇▇▇ 207.00avow: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 335.00deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 421.00 ==========================================================Test: defer-fulfill x 10000----------------------------------------------------------laissez: ▇ 21.00concurrent: ▇▇▇▇ 62.00when: ▇▇▇▇▇▇▇ 103.00avow: ▇▇▇▇▇▇▇▇▇ 142.00deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 424.00 ==========================================================Test: defer-reject x 10000----------------------------------------------------------avow: ▇▇▇ 50.00concurrent: ▇▇▇▇▇ 80.00when: ▇▇▇▇▇▇▇▇▇▇ 160.00laissez: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 240.00q: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 378.00 ==========================================================Test: defer-sequence x 10000----------------------------------------------------------laissez: | 7.00concurrent: ▇▇▇▇▇▇▇▇▇▇▇ 176.00when: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 209.00deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 271.00avow: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 352.00 ==========================================================Test: map x 10000----------------------------------------------------------deferred: ▇▇▇▇▇ 39.00concurrent: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 103.00when: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 385.00 ==========================================================Test: reduce-large x 10000NOTE: in node v0.8.14, deferred.reduce causes astack overflow for an array length >= 610----------------------------------------------------------concurrent: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 371.00when: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 502.00
You can find more performance results here
Overview
Simple Promise
Concurrent supports the bare bones Promise implementation that supports
then(onFulfilled, onRejected)
, fulfill(value)
, reject(reason)
.
var Promise = Promise; var success = ;success;success; var failure = ;failure;failure;
Futures
Concurrent also provides a Future class which inherits from Promise. It has a lot of syntactic sugar on top of the Promises/A+ spec based on the Scala Future API.
var Future = Future; var success = ;success
Collections
Concurrent also provides a collections library which gives a lot of the standard iterators. All the iterators are performed asynchronously and return Futures. The following methods are supported:
forEach
every
some
filter
map
reverse
reduce
reduceRight
Working with existing callbacks
Futures also have support for working with existing callback style libraries by
using the convert
method which returns a callback handler.
var Future = Future;var request = ; var google = ;google;;
Working with other Promise implementations
Since concurrent implements the Promises/A+ spec, it should work with other
libraries that implement the spec. Also, the Future class internally does not
expect any method beyond then
, fulfill
, and reject
so the prototype
methods can be merged into other implementations as long as they have those
three methods.
Browsers
Concurrent can be used in browser environments that support ES5, specifically
forEach
, Array.isArray
and Object.create
. Look at this JsFiddle
as an example of using the libary in the browser.
Credits
A lot of people had a hand in inspiring this project and helping getting it done. I'd like to thank:
- @brikis98 for helping me come up with the idea
- Promises, understanding by doing for the initial promise spec
- cujos/when for fully featured examples
- aheckmann/mpromise for node.js specific implementation
- promises-aplus/promises-tests for the test suite
- Scala Futures For API sugar
License
This is licensed under the MIT license. See the LICENSE file