re: Functional vs Imperative Patterns in JavaScript VIEW POST

FULL DISCUSSION
 

I found that for-loop sometimes is more readable than functional version, but functional version is better in most case.
For example, I will use for-or along with awaitwhen I want to await multiple promises one by one.

for(const url of urls){
  await fetch(url)
}

vs

await urls.reduce((p,url)=>p.then(()=>fetch(url)),Promise.resolve())
 

I agree with you about that example being easier to read. I would still prefer consistency in the app and ban all for loops in favor of map reduce and filter.

You could also break the one liner down a bit to help with readability and reusability.

const serialFetch = (promise, url) => promise.then(() => fetch(url))
await urls.reduce(serialFetch, Promise.resolve())
code of conduct - report abuse