Transforms a failed future into a new future.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
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();
}