bimap

Transforms the rejected or resolved values of a Task with a function. The state of the task is not changed.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

value(rejectionTransformation, successTransformation)
forall e1, e2, v1, v2:
  (Task e1 v1).((e1) => e2, (v1) => v2) => Task e2 v2

Documentation

Transforms the rejected or resolved values of a Task with a function. The state of the task is not changed.

Example:

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);
}

Properties

Source Code

Defined in source/concurrency/task/_task.js at line 20, column 0
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))
      });
    });
  }
Stability
experimental
Licence
MIT
Module
folktale/concurrency/task/_task
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)