noPromiseExecutorReturn (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noPromiseExecutorReturn - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-promise-executor-return
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noPromiseExecutorReturn": "error" } } }}Description
Section titled “Description”Disallow returning values from the function passed to new Promise().
When you create a Promise with new Promise(), you pass it a function called the executor.
The executor usually starts an asynchronous task, such as reading a file.
When the task finishes, the executor calls resolve with the result, or reject with an error.
new Promise() ignores the value that the executor returns, so returning a value has no effect on the Promise.
This is usually a mistake: the code most likely meant to call resolve or reject instead.
A return statement without a value is allowed, because it only stops the executor early.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”new Promise((resolve, reject) => { if (someCondition) { return defaultResult; } getSomething((err, result) => { if (err) { reject(err); } else { resolve(result); } });});code-block.js:3:9 lint/nursery/noPromiseExecutorReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The function passed to new Promise() returns a value here.
1 │ new Promise((resolve, reject) => {
2 │ if (someCondition) {
> 3 │ return defaultResult;
│ ^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │ getSomething((err, result) => {
ℹ new Promise() ignores this returned value, so it has no effect on the Promise.
ℹ Call resolve() or reject() to complete the Promise. To stop the function early, use return without a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
An arrow function without braces returns the value of its body:
new Promise((resolve, reject) => getSomething((err, data) => { if (err) { reject(err); } else { resolve(data); }}));code-block.js:1:34 lint/nursery/noPromiseExecutorReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The function passed to new Promise() returns this value.
> 1 │ new Promise((resolve, reject) => getSomething((err, data) => {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ if (err) {
> 3 │ reject(err);
> 4 │ } else {
> 5 │ resolve(data);
> 6 │ }
> 7 │ }));
│ ^^
8 │
ℹ new Promise() ignores this returned value, so it has no effect on the Promise.
ℹ Call resolve() or reject() to complete the Promise. To stop the function early, use return without a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Wrap the expression in braces.
1 │ - new·Promise((resolve,·reject)·=>·getSomething((err,·data)·=>·{
1 │ + new·Promise((resolve,·reject)·=>·{getSomething((err,·data)·=>·{
2 2 │ if (err) {
3 3 │ reject(err);
··· │
5 5 │ resolve(data);
6 6 │ }
7 │ - }));
7 │ + })});
8 8 │
new Promise(() => { return 1;});code-block.js:2:5 lint/nursery/noPromiseExecutorReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The function passed to new Promise() returns a value here.
1 │ new Promise(() => {
> 2 │ return 1;
│ ^^^^^^^^^
3 │ });
4 │
ℹ new Promise() ignores this returned value, so it has no effect on the Promise.
ℹ Call resolve() or reject() to complete the Promise. To stop the function early, use return without a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
new Promise(r => r(1));code-block.js:1:18 lint/nursery/noPromiseExecutorReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The function passed to new Promise() returns this value.
> 1 │ new Promise(r => r(1));
│ ^^^^
2 │
ℹ new Promise() ignores this returned value, so it has no effect on the Promise.
ℹ Call resolve() or reject() to complete the Promise. To stop the function early, use return without a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Wrap the expression in braces.
1 │ new·Promise(r·=>·{r(1)});
│ + +
new Promise((resolve, reject) => { if (someCondition) { resolve(defaultResult); return; } getSomething((err, result) => { if (err) { reject(err); } else { resolve(result); } });});
new Promise(r => { r(1) });
Promise.resolve(1);See Also
Section titled “See Also”- To disallow
asyncfunctions as the executor, seenoAsyncPromiseExecutor.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.