- From: James Browning <notifications@github.com>
- Date: Sat, 29 May 2021 00:00:08 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/981/850783404@github.com>
> > Do you find you need to "break out" with the `Promise` constructor as well?
I've personally used it quite a few times in particular for concurrency management, e.g. locks, (async) task queues, etc etc.
A typical example would be something like lock:
```js
class Deferred {
resolve;
reject;
promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
class Lock {
#locked = false;
#pendingRequests = [];
#release = () => {
assert(this.#locked);
const firstPending = this.#pendingRequests.shift();
if (firstPending) {
firstPending.resolve(once(this.#release));
} else {
this.#locked = false;
}
}
async acquire() {
if (this.#locked) {
const deferred = new Deferred();
this.#pendingRequests.push(deferred);
return await deferred.promise;
}
this.#locked = true;
return once(this.#release);
}
}
```
---
Although I can say one place I have specifically wanted the revealing constructor pattern on `AbortSignal` is to be able to subclass `AbortSignal`, which is otherwise undoable as `AbortSignal` has to be tied to a controller (so `super()` always throws, as `new AbortSignal()` always throws). It might be doable by some awful `super` override hacks, but real subclassing would be a lot nicer.
Although I wouldn't even need the subclass (actually a parallel implementation + extra methods) if a [few](https://github.com/whatwg/dom/issues/951) [suggestions](https://github.com/whatwg/dom/issues/948) [were](https://github.com/whatwg/dom/issues/946) [all](https://github.com/whatwg/dom/issues/920) [added](https://github.com/whatwg/dom/issues/927), the only reason I have the parallel implementation is that the listed issues are basically all things I use heavily when working with `AbortSignal` so it's just too cumbersome not to have them consistently available.
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/981#issuecomment-850783404
Received on Saturday, 29 May 2021 07:00:21 UTC