Skip to content

Commit

Permalink
fix: unhandled Promise rejections in pipeline.exec [skip ci] (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonAndre committed Nov 24, 2021
1 parent 2ee877e commit e5615da
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ Pipeline.prototype.execBuffer = deprecate(function () {
return execBuffer.apply(this, arguments);
}, "Pipeline#execBuffer: Use Pipeline#exec instead");

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

// In cluster mode, always load scripts before running the pipeline
if (this.isCluster) {
return pMap(scripts, (script) => _this.redis.script("load", script.lua), {
pMap(scripts, (script) => _this.redis.script("load", script.lua), {
concurrency: 10,
}).then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
})
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
})
.then(execPipeline, this.reject);
return this.promise;
}

return this.redis
this.redis
.script(
"exists",
scripts.map(({ sha }) => sha)
Expand All @@ -371,8 +381,9 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
})
.then(execPipeline, this.reject);
return this.promise;

function execPipeline() {
let data = "";
Expand Down

0 comments on commit e5615da

Please sign in to comment.