orElse

Transforms a failed future into a new future.

Signature

value(handler)
(Future 'f 's).(('f) => Future 'f2 's2) => Future 'f2 's

Documentation

Transforms a failed future into a new future.

Example:

const { rejected, of } = require('folktale/concurrency/future');

of(1).orElse(x => of(x + 1)).listen({
  onResolved: (value) => $ASSERT(value == 1)
});

rejected(1).orElse(x => of(x + 1)).listen({
  onResolved: (value) => $ASSERT(value == 2)
});

rejected(1).orElse(x => rejected(x + 1)).listen({
  onRejected: (value) => $ASSERT(value == 2)
});

Properties

Source Code

Defined in source/concurrency/future/_future.js at line 24, column 0
orElse(handler) {
    let deferred = new Deferred();      // eslint-disable-line prefer-const
    this.listen({
      onCancelled: ()     => deferred.cancel(),
      onResolved:  value  => deferred.resolve(value),
      onRejected:  reason => {
        handler(reason).listen({
          onCancelled: ()        => deferred.cancel(),
          onResolved:  value     => deferred.resolve(value),
          onRejected:  newReason => deferred.reject(newReason)
        });
      }
    });

    return deferred.future();
  }
Stability
stable
Licence
MIT
Module
folktale/concurrency/future/_future
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)