Execution should not fall through after reject call in a Promise executor

  • PROMISE_REJECT_FALL_THROUGH
  • Code Quality
  • Low
  • es6

This rule applies when execution continues after calling the reject function in a Promise executor.

The reject function of a Promise executor changes the state of the Promise, but does not stop the executor. In general, it is recommended to add a return statement after the reject to stop unintended code execution.

This rule detects the fall through case that the execution continues after a conditional reject and the resolve function is called eventually. In this case, the later resolve call has no effect if the reject has been called. Also, it is likely that the code executed between the reject and the resolve is meaningful only for normally resolved Promise. See Example 1 below.

It is possible that the code executed after a conditional reject is common to the resolve and reject cases like doing some cleanup. Even though no immediate problem occurs in this case, for code readability and maintainability, it is recommended to rearrange the conditional code so that the reject does not fall through to the resolve. See Example 2 below.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
let promise1 = new Promise((resolve, reject) => {
    fs.readFile('foo', (err, data) => {
        if (err) {
            reject(err); // PROMISE_REJECT_FALL_THROUGH alarm
        }
        console.log('Operation succeeded');
        resolve(data);
    });
});

// Example 2
let promise2 = new Promise((resolve, reject) => {
    fs.readFile('bar', (err, data) => {
        if (err) {
            reject(err); // PROMISE_REJECT_FALL_THROUGH alarm
        }
        doSomeCleanup();
        resolve(data);
    });
});

Compliant Code Example

View with noncompliant examples side by side
// Example 1
let promise1 = new Promise((resolve, reject) => {
    fs.readFile('foo', (err, data) => {
        if (err) {
            reject(err);
            return;
        }
        console.log('Operation succeeded');
        resolve(data);
    });
});

// Example 2
let promise2 = new Promise((resolve, reject) => {
    fs.readFile('bar', (err, data) => {
        if (err) {
            reject(err);
        } else {
            resolve(data);
        }
        doSomeCleanup();
    });
});

Version

This rule was introduced in DeepScan 1.46.0.

See

Was this documentation helpful?