-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(new convert future) Promise → Future conversion (#98)
- Loading branch information
1 parent
cf711e9
commit b8e5df4
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters