Skip to content

Commit e5615da

Browse files
authoredNov 24, 2021
fix: unhandled Promise rejections in pipeline.exec [skip ci] (#1466)
1 parent 2ee877e commit e5615da

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed
 

‎lib/pipeline.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,15 @@ Pipeline.prototype.execBuffer = deprecate(function () {
240240
return execBuffer.apply(this, arguments);
241241
}, "Pipeline#execBuffer: Use Pipeline#exec instead");
242242

243-
Pipeline.prototype.exec = function (callback: CallbackFunction) {
243+
// NOTE: To avoid an unhandled promise rejection, this will unconditionally always return this.promise,
244+
// which always has the rejection handled by standard-as-callback
245+
// adding the provided rejection callback.
246+
//
247+
// If a different promise instance were returned, that promise would cause its own unhandled promise rejection
248+
// errors, even if that promise unconditionally resolved to **the resolved value of** this.promise.
249+
Pipeline.prototype.exec = function (
250+
callback: CallbackFunction
251+
): Promise<Array<any>> {
244252
// Wait for the cluster to be connected, since we need nodes information before continuing
245253
if (this.isCluster && !this.redis.slots.length) {
246254
if (this.redis.status === "wait") this.redis.connect().catch(noop);
@@ -338,17 +346,19 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
338346

339347
// In cluster mode, always load scripts before running the pipeline
340348
if (this.isCluster) {
341-
return pMap(scripts, (script) => _this.redis.script("load", script.lua), {
349+
pMap(scripts, (script) => _this.redis.script("load", script.lua), {
342350
concurrency: 10,
343-
}).then(function () {
344-
for (let i = 0; i < scripts.length; i++) {
345-
_this.redis._addedScriptHashes[scripts[i].sha] = true;
346-
}
347-
return execPipeline();
348-
});
351+
})
352+
.then(function () {
353+
for (let i = 0; i < scripts.length; i++) {
354+
_this.redis._addedScriptHashes[scripts[i].sha] = true;
355+
}
356+
})
357+
.then(execPipeline, this.reject);
358+
return this.promise;
349359
}
350360

351-
return this.redis
361+
this.redis
352362
.script(
353363
"exists",
354364
scripts.map(({ sha }) => sha)
@@ -371,8 +381,9 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
371381
for (let i = 0; i < scripts.length; i++) {
372382
_this.redis._addedScriptHashes[scripts[i].sha] = true;
373383
}
374-
return execPipeline();
375-
});
384+
})
385+
.then(execPipeline, this.reject);
386+
return this.promise;
376387

377388
function execPipeline() {
378389
let data = "";

0 commit comments

Comments
 (0)
Please sign in to comment.