Transforms a failed future into a new future.
Transforms a failed future into a new future.
const { rejected, of } = require('folktale/concurrency/future');
of(1).orElse(x => of(x + 1)).listen({
onResolved: (value) => $ASSERT(value == 1)
});
rejected(1).orElse(x => of(x + 1)).listen({
onResolved: (value) => $ASSERT(value == 2)
});
rejected(1).orElse(x => rejected(x + 1)).listen({
onRejected: (value) => $ASSERT(value == 2)
});
orElse(handler) {
let deferred = new Deferred(); // eslint-disable-line prefer-const
this.listen({
onCancelled: () => deferred.cancel(),
onResolved: value => deferred.resolve(value),
onRejected: reason => {
handler(reason).listen({
onCancelled: () => deferred.cancel(),
onResolved: value => deferred.resolve(value),
onRejected: newReason => deferred.reject(newReason)
});
}
});
return deferred.future();
}