Transforms the value of a failed task.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
forall e1, e2, v:
(Task e1 v).((e1) => e2) => Task e2 v
Transforms the value of a failed task.
const { of, rejected } = require('folktale/concurrency/task');
const hello = of('hello').mapRejected(v => v.toUpperCase());
const result1 = await hello.run().promise();
$ASSERT(result1 == 'hello');
const hello2 = rejected('hello').mapRejected(v => v.toUpperCase());
try {
const result2 = await hello2.run().promise();
throw 'never happens';
} catch (error) {
$ASSERT(error == 'HELLO');
}
mapRejected(transformation) {
return new Task(resolver => {
const execution = this.run();
resolver.onCancelled(() => execution.cancel());
execution.listen({
onCancelled: resolver.cancel,
onRejected: reason => resolver.reject(transformation(reason)),
onResolved: resolver.resolve
});
});
}