Transforms the value of a successful 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 e, v1, v2:
(Task e v1).((v1) => v2) => Task e v2
Transforms the value of a successful task.
const { of, rejected } = require('folktale/concurrency/task');
const hello = of('hello').map(v => v.toUpperCase());
const result1 = await hello.run().promise();
$ASSERT(result1 == 'HELLO');
const hello2 = rejected('hello').map(v => v.toUpperCase());
try {
const result2 = await hello2.run().promise();
throw 'never happens';
} catch (error) {
$ASSERT(error == 'hello');
}
map(transformation) {
return new Task(resolver => {
const execution = this.run();
resolver.onCancelled(() => execution.cancel());
execution.listen({
onCancelled: resolver.cancel,
onRejected: resolver.reject,
onResolved: value => resolver.resolve(transformation(value))
});
});
}