Transforms both successful and failure values in a Future, without touching its state.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
(Future 'f 's).(('f) => 'f2, ('s) => 's2) => Future 'f2 's2
Transforms both successful and failure values in a Future, without touching its state.
const { of, rejected } = require('folktale/concurrency/future');
const inc = (x) => x + 1;
const dec = (x) => x - 1;
of(1).bimap(inc, dec).listen({
onResolved: (x) => $ASSERT(x == 0)
});
rejected(1).bimap(inc, dec).listen({
onRejected: (x) => $ASSERT(x == 2)
});
bimap(rejectionTransformation, successTransformation) {
let deferred = new Deferred(); // eslint-disable-line prefer-const
this.listen({
onCancelled: () => deferred.cancel(),
onRejected: reason => deferred.reject(rejectionTransformation(reason)),
onResolved: value => deferred.resolve(successTransformation(value))
});
return deferred.future();
}