Inverts the state of a Task. That is, turns successful tasks into failed ones, and failed tasks into successful ones.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
Inverts the state of a Task. That is, turns successful tasks into failed ones, and failed tasks into successful ones.
const { of, rejected } = require('folktale/concurrency/task');
try {
const result1 = await of(1).swap().run().promise();
throw 'never happens';
} catch (error) {
$ASSERT(error == 1);
}
const result2 = await rejected(1).swap().run().promise();
$ASSERT(result2 == 1);
swap() {
return new Task(resolver => {
let execution = this.run(); // eslint-disable-line prefer-const
resolver.onCancelled(() => execution.cancel());
execution.listen({
onCancelled: resolver.cancel,
onRejected: resolver.resolve,
onResolved: resolver.reject
});
});
}