orElse

Transforms a failed task's value and state.

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(handler)
forall e, e2, v:
  (Task e v).((e) => Task e2 v) => Task e2 v

Documentation

Transforms a failed task's value and state.

Example:

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

Properties

Source Code

Defined in source/concurrency/task/_task.js at line 20, column 0
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
          }));
        }
      });
    });
  }
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/)