From b72db7ebd66cb8f2a93e3a4f7eb564d657c08c89 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 2 Jan 2022 18:51:01 -0500 Subject: [PATCH] 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. --- src/node/utils/promises.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/node/utils/promises.js b/src/node/utils/promises.js index b9529d305..8d83514a8 100644 --- a/src/node/utils/promises.js +++ b/src/node/utils/promises.js @@ -59,9 +59,13 @@ exports.timesLimit = async (total, concurrency, promiseCreator) => { * An ordinary Promise except the `resolve` executor function is exposed as a property. */ 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; - super((resolve, reject) => { res = resolve; if (executor != null) executor(resolve, reject); }); + super((resolve, reject) => res = resolve); this.resolve = res; } }