Install with: npm install and run node app.
Comparing the use of foreach loop without lambdas and the use of a
collection pipeline (such as ....map(...).reduce(...)) with lambdas
and the resulting
sequential versus concurrent behavior.
async function fetchAndSum(...urls) {
let sum = 0
for (const url of urls) {
console.log(`FETCHING from ${url}`)
const res = await fetch(url)
const body = await res.text()
console.log(`=======> from ${url}`)
sum += body.length
}
return sum
} |
async function fetchAndSumλ(...urls) {
return urls
.map(async (url, i) => {
console.log(`FETCHING from ${url}`)
const resp = await fetch(url)
const body = await resp.text()
const length = body.length
console.log(`=======> from ${urls[i]}`)
return length
})
.reduce(async (prev, curr) => await prev + await curr)
} |
|
|