Navigation Menu

Skip to content

Commit

Permalink
Utility functions for converting to/from Promises
Browse files Browse the repository at this point in the history
These functions will make interoperating with existing async libraries
much easier.

Also bump npm version -> 1.0.10
  • Loading branch information
laverdet committed Feb 25, 2016
1 parent e09057a commit 4e31a39
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -534,6 +534,12 @@ Future.prototype.proxy = function(future) { ... }
* future threw, future.wait() will throw.
*/
Future.prototype.wait = function() { ... }

/**
* Support for converting a Future to and from ES6 Promises.
*/
Future.fromPromise = function(promise) { ... }
Future.prototype.promise = function() { ... }
```

GARBAGE COLLECTION
Expand Down
29 changes: 29 additions & 0 deletions future.js
Expand Up @@ -188,6 +188,19 @@ Future.wait = function wait(/* ... */) {
}
};

/**
* Return a Future that waits on an ES6 Promise.
*/
Future.fromPromise = function(promise) {
var future = new Future;
promise.then(function(val) {
future.return(val);
}, function(err) {
future.throw(err);
});
return future;
};

Future.prototype = {
/**
* Return the value of this future. If the future hasn't resolved yet this will throw an error.
Expand Down Expand Up @@ -408,6 +421,22 @@ Future.prototype = {
return this;
},

/**
* Returns an ES6 Promise
*/
promise: function() {
var that = this;
return new Promise(function(resolve, reject) {
that.resolve(function(err, val) {
if (err) {
reject(err);
} else {
resolve(val);
}
});
});
},

/**
* Differs from its functional counterpart in that it actually resolves the future. Thus if the
* future threw, future.wait() will throw.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "fibers",
"version": "1.0.9",
"version": "1.0.10",
"description": "Cooperative multi-tasking for Javascript",
"keywords": [
"fiber", "fibers", "coroutine", "thread", "async", "parallel", "worker", "future", "promise"],
Expand Down

0 comments on commit 4e31a39

Please sign in to comment.