π Disallow returning/yielding Promise.resolve/reject() in async functions or promise callbacks.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Wrapping a return value in Promise.resolve in an async function or a Promise#then/catch/finally callback is unnecessary as all return values in async functions and promise callback functions are already wrapped in a Promise. Similarly, returning an error wrapped in Promise.reject is equivalent to simply throwing the error. This is the same for yielding in async generators as well.
// β
const main = async foo => {
if (foo > 4) {
return Promise.reject(new Error('π€ͺ'));
}
return Promise.resolve(result);
};
// β
const main = async foo => {
if (foo > 4) {
throw new Error('π€ͺ');
}
return result;
};// β
async function * generator() {
yield Promise.resolve(result);
yield Promise.reject(error);
}
// β
async function * generator() {
yield result;
throw error;
}// β
promise
.then(x => {
if (x % 2 == 0) {
return Promise.resolve(x / 2);
}
return Promise.reject(new Error('odd number'));
})
.catch(error => Promise.reject(new FancyError(error)));
// β
promise
.then(x => {
if (x % 2 == 0) {
return x / 2;
}
throw new Error('odd number');
});
.catch(error => {
throw new FancyError(error);
});// β
promise.finally(() => Promise.reject(new Error('oh no')));
// β
promise.finally(() => {
throw new Error('oh no');
});