listen

Adds a visitor to the Future, which will be invoked when the Future's state changes.

Signature

value(pattern)
(Future 'f 's).(DeferredListener 'f 's) => Future 'f 's

Documentation

Adds a visitor to the Future, which will be invoked when the Future's state changes.

Listen allows reacting to changes in a Future, but is very imperative. In general, it's better to use willMatchWith or a more specialised method (like .chain or .orElse) instead.

Example:

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

of('hello').listen({
  onCancelled: ()       => { throw 'never happens' },
  onRejected:  (reason) => { throw 'never happens' },
  onResolved:  (value)  => { $ASSERT(value == 'hello') }
});

Properties

Source Code

Defined in source/concurrency/future/_future.js at line 24, column 0
listen(pattern) {
    this._state.matchWith({
      Pending:   ()           => this._listeners.push(pattern),
      Cancelled: ()           => pattern.onCancelled(), 
      Resolved:  ({ value })  => pattern.onResolved(value),
      Rejected:  ({ reason }) => pattern.onRejected(reason)
    });
    return this;
  }
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/)