promises: Return a `Promise` from `Gate.then()`

It doesn't make sense to return a `Gate` from `Gate.then()`, and this
eliminates the semantically confusing constructor parameter.
pull/5347/head
Richard Hansen 2022-01-02 18:51:01 -05:00
parent 78a67801f3
commit b72db7ebd6
1 changed files with 6 additions and 2 deletions

View File

@ -59,9 +59,13 @@ exports.timesLimit = async (total, concurrency, promiseCreator) => {
* An ordinary Promise except the `resolve` executor function is exposed as a property. * An ordinary Promise except the `resolve` executor function is exposed as a property.
*/ */
class Gate extends Promise { class Gate extends Promise {
constructor(executor = null) { // Coax `.then()` into returning an ordinary Promise, not a Gate. See
// https://stackoverflow.com/a/65669070 for the rationale.
static get [Symbol.species]() { return Promise; }
constructor() {
let res; let res;
super((resolve, reject) => { res = resolve; if (executor != null) executor(resolve, reject); }); super((resolve, reject) => res = resolve);
this.resolve = res; this.resolve = res;
} }
} }