Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f734510ce0a356b04cfce013aa005f9d to your computer and use it in GitHub Desktop.
Save anonymous/f734510ce0a356b04cfce013aa005f9d to your computer and use it in GitHub Desktop.
Testing a Cloudflare worker's fetch(). We want to run fetch() in the background without having to wait for a response, so we can respond immediately to the initial request.
addEventListener('fetch', event => {
event.respondWith(fetchAndFetch(event.request))
})
async function fetchAndFetch(request) {
const content = `timestamp=${Date.now()}`
// Actually a POST request.
console.log(content)
// Respond.
return new Response(`Sent a POST request with content: ${content} \n`)
}
// Monkey-patch console.log to fetch().
console.log = function() {
fetch('https://putsreq.com/ZyjM4ubsAADQyGBDRJ08', {
method: 'POST',
body: JSON.stringify([...arguments])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment