chain

Transforms a Future's successful value along with its 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(transformation)
(Future 'f 's).(('s) => Future 's2) => Future 'f 's2

Documentation

Transforms a Future's successful value along with its state.

Example:

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

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

of(1).chain(x => rejected(x)).listen({
  onRejected: (reason) => $ASSERT(reason == 1)
});

rejected(1).chain(x => of(x + 1)).listen({
  onRejected: (reason) => $ASSERT(reason == 1)
});

Properties

Source Code

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

    return deferred.future();
  }
Stability
experimental
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/)