# Prevent passing a function reference directly to iterator methods πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). Passing functions to iterator methods can cause issues when the function is changed without realizing that the iterator passes 2 more parameters to it. **This also applies when using TypeScript,** albeit only if the function accepts the same parameter type used by the iterator method. Suppose you have a `unicorn` module: ```js const unicorn = x => x + 1; export default unicorn; ``` You can then use it like this: ```js import unicorn from 'unicorn'; [1, 2, 3].map(unicorn); //=> [2, 3, 4] ``` The `unicorn` module now does a minor version that adds another argument: ```js const unicorn = (x, y) => x + (y ? y : 1); export default unicorn; ``` Your code will now return something different and probably break for users because it is now passing the index of the item as second argument. ```js import unicorn from 'unicorn'; [1, 2, 3].map(unicorn); //=> [2, 3, 5] ``` This rule helps safely call the function with the expected number of parameters: ```js import unicorn from 'unicorn'; [1, 2, 3].map(x => unicorn(x)); //=> [2, 3, 4] ``` ## Examples ```js // ❌ const foo = array.map(callback); // βœ… const foo = array.map(element => callback(element)); ``` ```js // βœ… const foo = array.map(Boolean); ``` ```js // ❌ array.forEach(callback); // βœ… array.forEach(element => { callback(element); }); ``` ```js // ❌ const foo = array.every(callback); // βœ… const foo = array.every(element => callback(element)); ``` ```js // ❌ const foo = array.filter(callback); // βœ… const foo = array.filter(element => callback(element)); ``` ```js // βœ… const foo = array.filter(Boolean); ``` ```js // ❌ const foo = array.find(callback); // βœ… const foo = array.find(element => callback(element)); ``` ```js // ❌ const index = array.findIndex(callback); // βœ… const index = array.findIndex(element => callback(element)); ``` ```js // ❌ const foo = array.some(callback); // βœ… const foo = array.some(element => callback(element)); ``` ```js // ❌ const foo = array.reduce(callback, 0); // βœ… const foo = array.reduce( (accumulator, element) => accumulator + callback(element), 0 ); ``` ```js // ❌ const foo = array.reduceRight(callback, []); // βœ… const foo = array.reduceRight( (accumulator, element) => [ ...accumulator, callback(element) ], [] ); ``` ```js // ❌ const foo = array.flatMap(callback); // βœ… const foo = array.flatMap(element => callback(element)); ``` ```js // ❌ array.forEach(someFunction({foo: 'bar'})); // βœ… const callback = someFunction({foo: 'bar'}); array.forEach(element => { callback(element); }); ``` ```js // ❌ array.forEach(callback, thisArgument); // βœ… array.forEach(function (element) { callback(element, this); }, thisArgument); ``` ```js // βœ… function readFile(filename) { return fs.readFile(filename, 'utf8'); } Promise.map(filenames, readFile); ```