Skip to content

Latest commit

 

History

History

js

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

JavaScript sample looking concurrency in fetchAndSum()

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)
}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831