Link Search Menu Expand Document

Fetch

Fetch data from an API.

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const result = await fetch('https://cat-fact.herokuapp.com/facts')
  const resultJson = await result.json()

  let fact = resultJson[Math.floor(Math.random() * resultJson.length)]
  
  return new Response(fact.text)
}

© 2021 Bip.sh, Ltd.