willMatchWith

Limited pattern matching for futures.

Signature

value(pattern)
forall a, b, c, d:
  type Pattern = { r |
    Cancelled: ()  => Future c d,
    Resolved:  (b) => Future c d,
    Rejected:  (a) => Future c d
  }
  
  (Future a b).(Pattern) => Future c d

Documentation

Limited pattern matching for futures.

Example:

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

of(1).willMatchWith({
  Cancelled: () => of('no'),
  Rejected:  (x) => of(x + 1),
  Resolved:  (x) => of(x - 1)
}).listen({
  onResolved: (x) => $ASSERT(x == 0)
});


rejected(1).willMatchWith({
  Cancelled: () => of('no'),
  Rejected:  (x) => of(x + 1),
  Resolved:  (x) => of(x - 1)
}).listen({
  onResolved: (x) => $ASSERT(x == 2)
})

Properties

Source Code

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

    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/)