Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Created August 21, 2018 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/83da279659c3d0a4e4144ce6d83ba371 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/83da279659c3d0a4e4144ce6d83ba371 to your computer and use it in GitHub Desktop.
Despite the code being identical other than the ordering of the "addEventListener" calls, the results change when run on the Cloudflare Worker platform.
addEventListener('fetch', event => {
console.log("fetch handler 2", event.request)
})
addEventListener('fetch', event => {
console.log("fetch handler 1")
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a given request object
* @param {Request} request
*/
async function handleRequest(request) {
console.log('Got request', request)
const response = await fetch(request)
console.log('Got response', response)
return response
}
/**
Outputs the following:
worker.js:2 fetch handler 2 Request {…}
worker.js:6 fetch handler 1
worker.js:16 Got request Request {…}
worker.js:18 Got response Response {…}
**/
addEventListener('fetch', event => {
console.log("fetch handler 1")
event.respondWith(handleRequest(event.request))
})
addEventListener('fetch', event => {
console.log("fetch handler 2", event.request)
})
/**
* Fetch and log a given request object
* @param {Request} request
*/
async function handleRequest(request) {
console.log('Got request', request)
const response = await fetch(request)
console.log('Got response', response)
return response
}
/**
Outputs the following:
worker.js:6 fetch handler 1
worker.js:15 Got request Request {…}
worker.js:17 Got response Response {…}
**/
@ThisIsMissEm
Copy link
Author

Apparently the cloudflare worker platform only executes 'fetch' event handlers until event.respondWith is called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment