Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Repository files navigation

Concurrent

Promises/A+ with Scala Awesomeness Build Status

Examples

varFuture=require('concurrent').Future;varrequest=require('request');varreq=function(options){varfuture=newFuture();request(options,future.convert(['res','body']));returnfuture;};/** * Example #1: Simple Async Call */// Fetch Google with a SLAvargoogle=req('http://google.com').ready(1000);// Fetch the statusvarstatus=google.map(function(value){returnvalue.res.statusCode;});// Log the final resultstatus.onSuccess(function(statusCode){console.log(statusCode);});/** * Examples #2: Parallel Calls */varduckDuckGo=req('http://duckduckgo.com');varbingOrYahoo=req('http://bing.com').fallbackTo(req('http://yahoo.com'));varasArray=Future.sequence([google,duckDuckGo,bingOrYahoo]);asArray.onSuccess(function(values){console.log(values);});varasObject=Future.sequence({google: google,duckDuckGo: duckDuckGo,bingOrYahoo: bingOrYahoo});varrecoverable=asObject.recover({promises: 'A+'});

Benchmarks

Here are the top 5 libraries for some of the tests

==========================================================
Test: promise-fulfill x 10000
----------------------------------------------------------
laissez: | 3.00
concurrent: ▇▇ 14.00
deferred: ▇▇ 19.00
when: ▇▇▇▇▇ 36.00
q: ▇▇▇▇▇▇▇▇ 57.00
==========================================================
Test: promise-reject x 10000
----------------------------------------------------------
concurrent: | 6.00
avow: ▇▇▇▇▇▇▇▇ 55.00
q: ▇▇▇▇▇▇▇▇▇ 60.00
when: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 89.00
laissez: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 118.00
==========================================================
Test: promise-sequence x 10000
----------------------------------------------------------
laissez: | 8.00
when: ▇▇▇▇▇▇▇▇ 204.00
concurrent: ▇▇▇▇▇▇▇▇ 207.00
avow: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 335.00
deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 421.00
==========================================================
Test: defer-fulfill x 10000
----------------------------------------------------------
laissez: ▇ 21.00
concurrent: ▇▇▇▇ 62.00
when: ▇▇▇▇▇▇▇ 103.00
avow: ▇▇▇▇▇▇▇▇▇ 142.00
deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 424.00
==========================================================
Test: defer-reject x 10000
----------------------------------------------------------
avow: ▇▇▇ 50.00
concurrent: ▇▇▇▇▇ 80.00
when: ▇▇▇▇▇▇▇▇▇▇ 160.00
laissez: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 240.00
q: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 378.00
==========================================================
Test: defer-sequence x 10000
----------------------------------------------------------
laissez: | 7.00
concurrent: ▇▇▇▇▇▇▇▇▇▇▇ 176.00
when: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 209.00
deferred: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 271.00
avow: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 352.00
==========================================================
Test: map x 10000
----------------------------------------------------------
deferred: ▇▇▇▇▇ 39.00
concurrent: ▇▇▇▇▇▇▇▇▇▇▇▇▇ 103.00
when: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 385.00
==========================================================
Test: reduce-large x 10000
NOTE: in node v0.8.14, deferred.reduce causes a
stack overflow for an array length >= 610
----------------------------------------------------------
concurrent: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 371.00
when: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 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).

varPromise=require('concurrent').Promise;varsuccess=newPromise();success.then(function(value){console.log(value);// 'success'});success.fulfill('success');varfailure=newPromise();failure.then(null,function(reason){console.log(reason);// 'failure'});failure.reject('failure');

Documentation

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.

varFuture=require('concurrent').Future;varsuccess=newFuture();success.onComplete(function(result){console.log(result;// 'success'});success.fulfill('success');varfailure=newFuture();failure.onComplete(function(result){console.log(result);// 'failure'});failure.reject('failure');

Documentation

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

Documentation

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.

varFuture=require('concurrent').Future;varrequest=require('request');vargoogle=newFuture();google.map(function(value){varbody=value[1];// Request returns a response and a bodyconsole.log(body);// HTML for http://www.google.com});request('http://www.google.com',google.convert());

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:

License

This is licensed under the MIT license. See the LICENSE file

Releases

Packages

Used by

Contributors

Languages