Transforms a Future's successful value along with its state.
(Future 'f 's).(('s) => Future 's2) => Future 'f 's2
Transforms a Future's successful value along with its state.
const { of, rejected } = require('folktale/concurrency/future');
of(1).chain(x => of(x + 1)).listen({
onResolved: (value) => $ASSERT(value == 2)
});
of(1).chain(x => rejected(x)).listen({
onRejected: (reason) => $ASSERT(reason == 1)
});
rejected(1).chain(x => of(x + 1)).listen({
onRejected: (reason) => $ASSERT(reason == 1)
});
chain(transformation) {
let deferred = new Deferred(); // eslint-disable-line prefer-const
this.listen({
onCancelled: () => deferred.cancel(),
onRejected: reason => deferred.reject(reason),
onResolved: value => {
transformation(value).listen({
onCancelled: () => deferred.cancel(),
onRejected: reason => deferred.reject(reason),
onResolved: value2 => deferred.resolve(value2)
});
}
});
return deferred.future();
}