Skip to content

Commit

Permalink
Handle possible unhandled promise rejection with autopipelining+cluster
Browse files Browse the repository at this point in the history
Deliberately add `asCallback` on the **same** Promise instance being returned,
not on a different Promise instance that resolves to the same thing.

Avoid this error:

```javascript
process.on('unhandledRejection', (reason) => console.log('unhandledRejection', reason));
const x = Promise.reject(new Error());
x.catch((e) => console.log('caught x', e));

const causesUnhandledRejection = Promise.resolve().then(() => x);
```

Related to #1466
  • Loading branch information
TysonAndre committed Nov 23, 2021
1 parent 77ad094 commit 3acf1e8
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/autoPipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,24 @@ export function executeWithAutoPipelining(
// On cluster mode let's wait for slots to be available
if (client.isCluster && !client.slots.length) {
if (client.status === "wait") client.connect().catch(noop);
return new CustomPromise(function (resolve, reject) {
client.delayUntilReady((err) => {
if (err) {
reject(err);
return;
}

executeWithAutoPipelining(
client,
functionName,
commandName,
args,
callback
).then(resolve, reject);
});
});
return asCallback(
new CustomPromise(function (resolve, reject) {
client.delayUntilReady((err) => {
if (err) {
reject(err);
return;
}

executeWithAutoPipelining(
client,
functionName,
commandName,
args
).then(resolve, reject);
});
}),
callback
);
}

// If we have slot information, we can improve routing by grouping slots served by the same subset of nodes
Expand Down

0 comments on commit 3acf1e8

Please sign in to comment.