Ensure that promises are either resolved or rejected, but never both. When handling promise resolution in callbacks or event handlers, use proper conditional logic to guarantee mutual exclusion between success and error paths.
Ensure that promises are either resolved or rejected, but never both. When handling promise resolution in callbacks or event handlers, use proper conditional logic to guarantee mutual exclusion between success and error paths.
For example, this pattern creates inconsistent promise states:
return new Promise((resolve, reject) => {
child.on("message", (msg) => {
if (msg.error) {
reject();
}
resolve(); // Problem: This always executes regardless of error state
});
});
Instead, use an else block to ensure mutual exclusion:
return new Promise((resolve, reject) => {
child.on("message", (msg) => {
if (msg.error) {
reject();
} else {
resolve(); // Correct: Only resolves if there is no error
}
});
});
This prevents inconsistent promise states where both error and success handlers might execute, leading to unpredictable application behavior and hard-to-debug issues.
Enter the URL of a public GitHub repository