Transforms a failed task's value and state.
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 e, e2, v:
(Task e v).((e) => Task e2 v) => Task e2 v
Transforms a failed task's value and state.
const { of, rejected } = require('folktale/concurrency/task');
const hello = of('hello').orElse(error => of('world'));
const result1 = await hello.run().promise();
$ASSERT(result1 == 'hello');
const world = rejected('hello').orElse(error => of('world'));
const result2 = await world.run().promise();
$ASSERT(result2 == 'world');
const helloWorld = rejected('hello').orElse(error => rejected(error + ' world'));
try {
const result3 = await helloWorld.run().promise();
throw 'never happens';
} catch (error) {
$ASSERT(error == 'hello world');
}
orElse(handler) {
return new Task(resolver => {
const execution = this.run();
resolver.onCancelled(() => execution.cancel());
execution.listen({
onCancelled: resolver.cancel,
onResolved: resolver.resolve,
onRejected: reason => {
execution.link(handler(reason).run().listen({
onCancelled: resolver.cancel,
onRejected: resolver.reject,
onResolved: resolver.resolve
}));
}
});
});
}