Inverts the state of a Future: successes become failures, failures become successes.
Inverts the state of a Future: successes become failures, failures become successes.
const { of, rejected } = require('folktale/concurrency/future');
of(1).swap().listen({
onRejected: (x) => $ASSERT(x == 1)
});
rejected(1).swap().listen({
onResolved: (x) => $ASSERT(x == 1)
});
swap() {
let deferred = new Deferred(); // eslint-disable-line prefer-const
this.listen({
onCancelled: () => deferred.cancel(),
onRejected: reason => deferred.resolve(reason),
onResolved: value => deferred.reject(value)
});
return deferred.future();
}