Transforms the rejected or resolved values of a Task with a function. The state of the task is not changed.
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, v1, v2:
(Task e1 v1).((e1) => e2, (v1) => v2) => Task e2 v2
Transforms the rejected or resolved values of a Task with a function. The state of the task is not changed.
const { of, rejected } = require('folktale/concurrency/task');
const result1 = await of(1).bimap(
(error) => error + 1,
(success) => success - 1
).run().promise();
$ASSERT(result1 == 0);
try {
const result2 = await rejected(1).bimap(
(error) => error + 1,
(success) => success - 1
).run().promise();
throw 'never happens';
} catch (error) {
$ASSERT(error == 2);
}
bimap(rejectionTransformation, successTransformation) {
return new Task(resolver => {
const execution = this.run();
resolver.onCancelled(() => execution.cancel());
execution.listen({
onCancelled: resolver.cancel,
onRejected: reason => resolver.reject(rejectionTransformation(reason)),
onResolved: value => resolver.resolve(successTransformation(value))
});
});
}