haskellcamargo / js-real-world-functional-programming

Tips and guidelines for real world functional code-bases in JS
MIT License
356 stars 14 forks source link

callbacks: return new fetch promise #4

Closed felquis closed 6 years ago

felquis commented 6 years ago

The standard fetch function does not have a tap either, it is bluebird specific method, but I just ignored that fact for simplicity.

For the browser it would be something like:

const toJson = response => response.json()
const tap = data => {
  console.log(data)
  return data
}

fetch('https://api.ipify.org?format=json')
  .then(toJson)
  .then(({ ip }) => fetch(`https://geoip.nekudo.com/api/${ip}/en`))
  .then(toJson)
  .then(tap)