The resolve and reject function should not be called multiple times in a Promise executor

  • MULTIPLE_RESOLVE_IN_PROMISE_EXECUTOR
  • Error
  • Medium
  • es6

This rule applies when the resolve or reject function of a Promise executor is called multiple times.

Once the resolve or reject is called, the state of the Promise can no longer be changed. If it is called multiple times, the later ones will have no effect.

Even when no immediate problem occurs, it is recommended to check the code logic and remove unintended ones among the multiple calls.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
let promise1 = new Promise((resolve, reject) => {
    fs.readFile('foo', (err, data) => {
        resolve(data);
        if (err) {
            reject(err); // MULTIPLE_RESOLVE_IN_PROMISE_EXECUTOR alarm because 'resolve' has already been called.
        }
    });
});

// Example 2
let promise2 = new Promise((resolve, reject) => {
    fs.readFile('bar', (err, data) => {
        if (err) {
            console.log(err);
            resolve('');
            reject(err); // MULTIPLE_RESOLVE_IN_PROMISE_EXECUTOR alarm because 'resolve' has already been called.
        } else {
            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;
        }
        resolve(data);
    });
});

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

Version

This rule was introduced in DeepScan 1.46.0.

See

Was this documentation helpful?