From 65e893e306f97fc4fbf1f12d126ba3141dafde48 Mon Sep 17 00:00:00 2001 From: laohan <120936337@qq.com> Date: Fri, 6 Dec 2024 00:09:45 +0800 Subject: [PATCH] Fix cleaning up abort listener (#81) --- index.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2f7d91c..1055800 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ export default async function pMap( signal, } = {}, ) { - return new Promise((resolve, reject_) => { + return new Promise((resolve_, reject_) => { if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) { throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`); } @@ -30,10 +30,24 @@ export default async function pMap( let currentIndex = 0; const iterator = iterable[Symbol.iterator] === undefined ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator](); + const signalListener = () => { + reject(signal.reason); + }; + + const cleanup = () => { + signal?.removeEventListener('abort', signalListener); + }; + + const resolve = value => { + resolve_(value); + cleanup(); + }; + const reject = reason => { isRejected = true; isResolved = true; reject_(reason); + cleanup(); }; if (signal) { @@ -41,9 +55,7 @@ export default async function pMap( reject(signal.reason); } - signal.addEventListener('abort', () => { - reject(signal.reason); - }); + signal.addEventListener('abort', signalListener, {once: true}); } const next = async () => {