no-promise-executor-return
Disallow returning a value from the function passed to new Promise
has suggestions
new Promise ignores whatever its function returns. A return value there usually means you meant resolve(value), and the promise never settles.
An arrow function without braces also returns a value. For example, resolve => setTimeout(resolve, 100) returns the timer. That one's harmless, but it's reported too. Put the body in braces.
A return without a value is allowed, so you can still leave the function early.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-promise-executor-return" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-promise-executor-return" = ["error", {
allowVoid = false,
}]Examples#
example.js2 problems
function readCached(key) {
return new Promise((resolve, reject) => {
if (cache.has(key)) {
return cache.get(key);Return values from promise executor functions cannot be read.
}
load(key, resolve, reject);
});
}
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)Return values from promise executor functions cannot be read.);example.jsNo problems
function readCached(key) {
return new Promise((resolve, reject) => {
if (cache.has(key)) {
resolve(cache.get(key));
return;
}
load(key, resolve, reject);
});
}
const wait = ms =>
new Promise(resolve => {
setTimeout(resolve, ms);
});Options#
| Option | Default | What it does |
|---|---|---|
allowVoid | false | Allow returning a void expression, which says you're dropping the value on purpose. |
With { allowVoid: true }, this is allowed:
example.jswith
{ "allowVoid": true }No problemsconst wait = ms => new Promise(resolve => void setTimeout(resolve, ms));Without the option, that line is reported:
example.js1 problem
const wait = ms => new Promise(resolve => void setTimeout(resolve, ms)Return values from promise executor functions cannot be read.);