Constructor
new FlutureTMonetEither(monad) → {FlutureTMonetEither}
- Source:
Constructor for transforming monads. Call also be used as static function without calling new statement.
Example
EitherT(Identity.of(1)); //=> Either.Right(1)
EitherT(Either.Right(1)); //=> FlutureTMonetEither(1)
EitherT(Future.of(1)); //=> FlutureTMonetEither(1)
EitherT.of(Maybe.Some(1)); //=> EitherT<Maybe.Some(1)
Parameters:
Name | Type | Description |
---|---|---|
monad |
Identity | Either | Future |
Throws:
Returns:
- Type
- FlutureTMonetEither
Members
(static) '@@type' :string
- Source:
Type:
- string
Example
const { isFlutureTMonetEither } = require('monad-t/lib/FlutureTMonetEither/utils');
const FlutureTMonetEither = require('monad-t/lib/FlutureTMonetEither');
isFlutureTMonetEither(FlutureTMonetEither.fromValue(1)); //=> true
(static) encaseP
- Source:
Allows Promise-returning functions to be turned into FlutureTMonetEither-returning functions.
Takes a function which returns a Promise, and a value, and returns a FlutureTMonetEither. When forked, the FlutureTMonetEither calls the function with the value to produce the Promise, and resolves with its resolution value, or rejects with its rejection reason.
Partially applying encase with a function f allows us to create a "safe" version of f. Instead of throwing exceptions, the encased version always returns a FlutureTMonetEither when given the remaining argument(s).
(static) encaseP2
- Source:
Binary version of encaseP
.
(static) encaseP3
- Source:
Ternary version of encaseP
.
Methods
(static) both(futureEitherA, futureEitherB) → {FlutureTMonetEither}
- Source:
Run two FlutureTMonetEithers in parallel. Basically like calling FlutureTMonetEither.parallel with exactly two FlutureTMonetEithers.
Parameters:
Name | Type | Description |
---|---|---|
futureEitherA |
FlutureTMonetEither | |
futureEitherB |
FlutureTMonetEither |
Returns:
- Type
- FlutureTMonetEither
(static) cache(futureEither) → {FlutureTMonetEither}
- Source:
Returns a Future which caches the resolution value of the given Future so that whenever it's forked, it can load the value from cache rather than re-executing the chain.
Example
const loadDbRecordById = (id) => {
console.log('Retrieving record from db');
return DB.record.findById(id);
}
const loadFromDb = FlutureTMonetEither.cache(
FlutureTMonetEither.encaseP(loadSomethingFromDbById, 1)
);
loadFromDb.fork(console.error, console.log);
//> "Retrieving record from db"
//> {...record...}
loadFromDb.fork(console.error, console.log);
//> {...record...}
Parameters:
Name | Type | Description |
---|---|---|
futureEither |
FlutureTMonetEither |
Returns:
- Type
- FlutureTMonetEither
(static) do(generator) → {FlutureTMonetEither}
- Source:
A specialized version of fantasy-do which works only for Futures, but has the advantage of type-checking and not having to pass FlutureTMonetEither.of. Another advantage is that the returned FlutureTMonetEither can be forked multiple times, as opposed to with a general fantasy-do solution, where forking the FlutureTMonetEither a second time behaves erroneously.
Takes a function which returns an Iterator, commonly a generator-function, and chains every produced FlutureTMonetEither over the previous.
This allows for writing sequential asynchronous code without the pyramid of doom. It's known as "coroutines" in Promise land, and "do-notation" in Haskell land.
Parameters:
Name | Type | Description |
---|---|---|
generator |
function |
Returns:
- Type
- FlutureTMonetEither
(static) fromEither(either) → {FlutureTMonetEither}
- Source:
Creates FlutureTMonetEither instance from an either, running it thought the following composition: FlutureTMonetEither.of * Fluture.of
Example
FlutureTMonetEither.fromEither(Either.Right(1)); //=> FlutureTMonetEither(1)
Parameters:
Name | Type | Description |
---|---|---|
either |
Either |
Returns:
- Type
- FlutureTMonetEither
(static) fromFuture(future) → {FlutureTMonetEither}
- Source:
Creates FlutureTMonetEither instance from a future, transforming the value locked inside the future into Either.Right, and then wrapping the future into FlutureTMonetEither.
Example
FlutureTMonetEither.fromFuture(Future.of(1)); //=> FlutureTMonetEither(1)
Parameters:
Name | Type | Description |
---|---|---|
future |
Future |
Returns:
- Type
- FlutureTMonetEither
(static) fromValue(val) → {FlutureTMonetEither}
- Source:
Creates FlutureTMonetEither instance from a value, running a value through the following composition: FlutureTMonetEither.of Fluture.of Either.Right
Example
FlutureTMonetEither.fromValue(1); //=> FlutureTMonetEither(1)
Parameters:
Name | Type | Description |
---|---|---|
val |
* |
Returns:
- Type
- FlutureTMonetEither
(static) go(generator) → {FlutureTMonetEither}
- Source:
Alias of do
.
Parameters:
Name | Type | Description |
---|---|---|
generator |
function |
Returns:
- Type
- FlutureTMonetEither
(static) of(runnon-null) → {FlutureTMonetEither}
- Source:
Creates a FlutureTMonetEither transformer from Either locked inside a Future.
Example
FlutureTMonetEither.of(Future.of(Either.Right(1))); //=> FlutureTMonetEither(1)
Parameters:
Name | Type | Description |
---|---|---|
run |
Future | Either wrapped in Future |
Returns:
- Type
- FlutureTMonetEither
(static) parallel(runInParallelCount, futureEitherList) → {FlutureTMonetEither}
- Source:
Creates a Future which when forked runs all Futures in the given array in parallel, ensuring no more than limit Futures are running at once.
Parameters:
Name | Type | Description |
---|---|---|
runInParallelCount |
number | |
futureEitherList |
Array.<FlutureTMonetEither> |
Returns:
- Type
- FlutureTMonetEither
(static) tryP(fn) → {FlutureTMonetEither}
- Source:
Create a FlutureTMonetEither which when forked spawns a Promise using the given function and resolves with its resolution value, or rejects with its rejection reason.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- FlutureTMonetEither
and(futureEither) → {FlutureTMonetEither}
- Source:
Returns a new FlutureTMonetEither which either rejects with the first rejection reason, or resolves with the last resolution value once and if both FlutureTMonetEither resolve. This behaves analogously to how JavaScript's and-operator does.
Parameters:
Name | Type | Description |
---|---|---|
futureEither |
FlutureTMonetEither |
Returns:
- Type
- FlutureTMonetEither
chain(fn) → {FlutureTMonetEither}
- Source:
Allows the creation of a new FlutureTMonetEither based on the resolution value. This is like doing promise.then(x => Promise.resolve(x + 1)), except that it's lazy, so the new FlutureTMonetEither will not be created until the other one is forked. The function is only ever applied to the resolution value; it's ignored when the FlutureTMonetEither was rejected. To learn more about the exact behaviour of chain.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- FlutureTMonetEither
chainEither(fn) → {FlutureTMonetEither}
- Source:
Method for chaining inner Either with an either returned by
the supplied fn
.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function generating new Either |
Returns:
- Type
- FlutureTMonetEither
chainFuture(fn) → {FlutureTMonetEither}
- Source:
Method for chaining inner Future with an Future returned by
the supplied fn
. Future instance should not have value inside it, not Either.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function generating new Future |
Returns:
- Type
- FlutureTMonetEither
filter(predicate, fnOrValue) → {FlutureTMonetEither}
- Source:
Very similar to the filtering of a list, filter on FlutureTMonetEither will filter out any elements that do not meet the predicate.
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | |
fnOrValue |
function | * |
Returns:
- Type
- FlutureTMonetEither
fork(leftFn, rightFn) → {FlutureTMonetEither}
- Source:
Execute the computation that was passed to the FlutureTMonetEither at construction using the given reject and resolve callbacks.
Parameters:
Name | Type | Description |
---|---|---|
leftFn |
function | |
rightFn |
function |
Returns:
- Type
- FlutureTMonetEither
map(fn) → {FlutureTMonetEither}
- Source:
Transforms the resolution value inside the FlutureTMonetEither, and returns a new FlutureTMonetEither with the transformed value. This is like doing promise.then(x => x + 1), except that it's lazy, so the transformation will not be applied before the FlutureTMonetEither is forked. The transformation is only applied to the resolution branch: If the FlutureTMonetEither is rejected, the transformation is ignored. *
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- FlutureTMonetEither
mapRej(fn) → {FlutureTMonetEither}
- Source:
Map over the rejection reason of the FlutureTMonetEither. This is like map, but for the rejection branch.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- FlutureTMonetEither
promise() → {Promise}
- Source:
An alternative way to fork the FlutureTMonetEither. This eagerly forks the FlutureTMonetEither and returns a Promise of the result. This is useful if some API wants you to give it a Promise. It's the only method which forks the Future without a forced way to handle the rejection branch, so I recommend against using it for anything else.
Returns:
- Type
- Promise
tap(fn) → {FlutureTMonetEither}
- Source:
Method for isolating side effects.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- FlutureTMonetEither
tapF(fn) → {FlutureTMonetEither}
- Source:
Method for isolating asynchronous side effects.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function returning new instance of FlutureTMonetEither. |
Returns:
- Type
- FlutureTMonetEither