Skip to content

useIterableCallbackReturn

Enforce consistent return values in iterable callbacks.

This rule ensures that callbacks passed to certain iterable methods either always return a value or never return a value, depending on the method’s requirements.

Methods and Their Requirements

Section titled Methods and Their Requirements

The following methods require a return in their callback:

  • every
  • filter
  • find
  • findIndex
  • findLast
  • findLastIndex
  • flatMap
  • map
  • reduce
  • reduceRight
  • some
  • sort
  • toSortedfrom (when called on Array)

A return value is disallowed in the method forEach.

[].map(() => {
// Missing return value
});
code-block.js:1:4 lint/nursery/useIterableCallbackReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This callback passed to map() iterable method should always return a value.

> 1 │ [].map(() => {
^^^
2 │ // Missing return value
3 │ });

Add a return with a value to this callback.

[].forEach(() => {
return 1; // Should not return a value
});
code-block.js:1:4 lint/nursery/useIterableCallbackReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This callback passed to forEach() iterable method should not return a value.

> 1 │ [].forEach(() => {
^^^^^^^
2 │ return 1; // Should not return a value
3 │ });

Either remove this return or remove the returned value.

> 1 │ [].forEach(() => {

> 2 │ return 1; // Should not return a value
^^^^^^^
3 │ });
4 │

[].map(() => {
return 1; // Correctly returns a value
});
[].forEach(() => {
// No return value, which is correct
});
biome.json
{
"linter": {
"rules": {
"nursery": {
"useIterableCallbackReturn": "error"
}
}
}
}