admin管理员组文章数量:1389824
Is it legit (or good practice) do a loop with a higher order function - like Array.map()
- to perform some side effects ?
This is mostly a theoretical question since I notice that sometimes I found (or perform myself) some loops using the .map()
method, like this:
let myObject = {...}
let myItemsArray = [ ... , ... ]
myItemsArray.map( (item, index) => {
// do some side effect for ex:
myObject.item[index] = item
}
But I know that this map()
method actually returns an array. So calling myItemsArray.map()
is like I'm returning an array without assign it to any variable.
So the question, is this legit? Should I avoid this and use a classic for()
loop or a .forEach()
method?
Side question: one of the reasons I'm asking this is because I need to perform some loops on an async function so promises and await operators are involved, I remember that forEach()
loops are not ideal on an async function, but wanted to know why.
Is it legit (or good practice) do a loop with a higher order function - like Array.map()
- to perform some side effects ?
This is mostly a theoretical question since I notice that sometimes I found (or perform myself) some loops using the .map()
method, like this:
let myObject = {...}
let myItemsArray = [ ... , ... ]
myItemsArray.map( (item, index) => {
// do some side effect for ex:
myObject.item[index] = item
}
But I know that this map()
method actually returns an array. So calling myItemsArray.map()
is like I'm returning an array without assign it to any variable.
So the question, is this legit? Should I avoid this and use a classic for()
loop or a .forEach()
method?
Side question: one of the reasons I'm asking this is because I need to perform some loops on an async function so promises and await operators are involved, I remember that forEach()
loops are not ideal on an async function, but wanted to know why.
-
2
Building an array you don't need is pointless, use
forEach
instead. But if you are doing asynchronous things it's possible you do need the array, because that's whatPromise.all
takes to resolve all of the promises. – jonrsharpe Commented Dec 14, 2020 at 9:29 -
1
you should just use
forEach
if you do'nt intend to actually map anything to a new array – TKoL Commented Dec 14, 2020 at 9:29 - @jonrsharpe thanks, indeed my code resolves some promises so the Promise.All() takes the result in the end – Santiago Commented Dec 14, 2020 at 9:36
- 1 Is performing a mapping operation without using returned value an antipattern? – VLAZ Commented Dec 14, 2020 at 9:38
1 Answer
Reset to default 6So the question, is this legit? Should I avoid this and use a classic for() loop or a .forEach() method?
If you aren't returning anything from the map function, then you should use forEach
instead. You end up with the same result but you don't imply, to anyone maintaining your code, that you are returning something useful.
one of the reasons I'm asking this is because I need to perform some loops on an async function so promises and await operators are involved, I remember that forEach() loops are not ideal on an async function, but wanted to know why.
forEach()
is generally not remended for use with promises because it lacks any feature that can be used to wait for them to resolve. map()
, by itself, has the same problem (but see the final scenario below).
There are three ways I would handle promises in a loop depending on what timing behaviour I wanted from the loop and the code that followed it.
The items in the loop need to handled in series
Use a regular for ()
loop so that the function pauses while each promise is awaited.
for (let i = 0; i < array.length; i++) {
await somethingUsing(array[i]);
}
doSomethingWhenAllPromisesAreSettled();
The items in the loop can be handled in parallel and the code that es after doesn't need to wait for it
Use a forEach
.
array.forEach((item) => {
somethingUsing(item);
});
doSomethingWhileThePromisesAreInFlight();
The items in the loop can be handled in parallel but the code that es after does need to wait for them
Use a map
. Pass the array of returned promises to Promise.all
. Then await
that.
await Promise.all(array.map((item) => {
return somethingUsing(item);
});
doSomethingWhenAllPromisesAreSettled();
本文标签: javascriptIs it legit to use map() method only for sideeffectsStack Overflow
版权声明:本文标题:javascript - Is it legit to use .map() method only for side-effects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744740767a2622592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论