Skip to content

Commit

Permalink
(new convert future) Promise → Future conversion (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed May 4, 2017
1 parent cf711e9 commit b8e5df4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/data/conversions/promise-to-future.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//----------------------------------------------------------------------
//
// This source file is part of the Folktale project.
//
// Licensed under MIT. See LICENCE for full licence information.
// See CONTRIBUTORS for the list of contributors to the project.
//
//----------------------------------------------------------------------

const { Cancelled } = require('folktale/data/future/_execution-state');
const Deferred = require('folktale/data/future/_deferred');

/*~
* stability: experimental
* type: |
* forall e, v:
* (Promise v e) => Future e v
*/
const promiseToFuture = (aPromise) => {
const deferred = new Deferred();
aPromise.then(
(value) => deferred.resolve(value),
(error) => {
if (Cancelled.hasInstance(error)) {
deferred.cancel();
} else {
deferred.reject(error);
}
}
)
return deferred.future();
};


module.exports = promiseToFuture;
10 changes: 10 additions & 0 deletions src/data/future/_future.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ Object.assign(Future, {
let result = new Future(); // eslint-disable-line prefer-const
result._state = Rejected(reason);
return result;
},


/*~
* stability: experimental
* type: |
* forall e, v: (Promise v e) => Future e v
*/
fromPromise(aPromise) {
return require('folktale/data/conversions/promise-to-future')(aPromise);
}
});

Expand Down
1 change: 1 addition & 0 deletions src/data/future/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Future = require('./_future');
module.exports = {
of: Future.of,
rejected: Future.rejected,
fromPromise: Future.fromPromise,
_Deferred: require('./_deferred'),
_ExecutionState: require('./_execution-state'),
_Future: Future
Expand Down
33 changes: 33 additions & 0 deletions test/specs-src/data.future.es6
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ const { _ExecutionState, _Deferred: Deferred } = Future;
const { Resolved, Rejected } = _ExecutionState;

describe('Data.Future', function() {
describe('conversions', () => {
property('Promise.resolve(v) → Future.resolve(v)', 'nat', (a) => {
return new Promise((ok, error) => {
Future.fromPromise(Promise.resolve(a)).listen({
onResolved: (v) => v === a ? ok(true) : error(`Assertion failed: ${a} === ${v}`),
onRejected: (v) => error(`Expected a resolved future with ${a}, got a rejected future with ${v}`),
onCancelled: () => error(`Expected a resolved future with ${a}, got a cancelled future`)
});
});
});

property('Promise.reject(v) → Future.reject(v)', 'nat', (a) => {
return new Promise((ok, error) => {
Future.fromPromise(Promise.reject(a)).listen({
onRejected: (v) => v === a ? ok(true) : error(`Assertion failed: ${a} === ${v}`),
onResolved: (v) => error(`Expected a rejected future with ${a}, got a resolved future with ${v}`),
onCancelled: () => error(`Expected a rejected future with ${a}, got a cancelled future`)
});
});
});

property('Promise.reject(Cancelled()) → Future.cancel()', () => {
return new Promise((ok, error) => {
Future.fromPromise(Promise.reject(_ExecutionState.Cancelled())).listen({
onRejected: (v) => error(`Expected a cancelled future, got a rejected future with ${v}`),
onResolved: (v) => error(`Expected a cancelled future, got a resolved future with ${v}`),
onCancelled: () => ok(true)
});
});
});
});


describe('Deferreds', function() {
it('new Deferred() should start in a pending state', () => {
let deferred = new Deferred();
Expand Down

0 comments on commit b8e5df4

Please sign in to comment.