Cool, yeah, the reveal pattern would indeed be a good way to guard against unwanted/unexpected cancels.
```js class Cancel { constructor(executor) { this._requested = false executor(() => this._requested = true) } get requested() {return this._requested} throw() { throw new CancelError() } } let cancel = new Cancel(function(cancel) { cancelButton.onclick = cancel }) ``` What would be the recommended way of keeping the internal state private? With a WeakMap? On Mon, Jan 4, 2016 at 1:01 PM, Kevin Smith <zenpars...@gmail.com> wrote: >> Since checking `promise.state` is synchronous, we may as well just >> write a synchronous Cancel class instead: > > > Right - see upthread. You do need some kind of callback method, though, > like `whenCancelled(callback)`. > >> >> class Cancel { >> constructor() { this.requested = false } >> request() { this.requested = true } >> throw() { throw new CancelError() } >> } > > > We need to separate the capability to "read" the cancellation request from > the ability to request the cancellation. That's why in .NET you have > CancellationTokenSource and CancellationToken. But for JS, we should > probably use the revealing constructor pattern instead (discussed upthread): > > let token = new CancelToken(cancel => { ... }); > _______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss