Adds a visitor to the Future, which will be invoked when the Future's state changes.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
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.
const { of } = require('folktale/concurrency/future');
of('hello').listen({
onCancelled: () => { throw 'never happens' },
onRejected: (reason) => { throw 'never happens' },
onResolved: (value) => { $ASSERT(value == 'hello') }
});
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;
}