Hi @tallyfy,
const res = await fetch(request)
is the correct version. fetch()
returns a promise for a response – to get the actual response, it must either be await
ed or have .then()
called on it, like fetch(request).then(response => { if response.status === 404) ... }
. Using await
is typically the more natural of the two options, as the code turns out easier to read.
event.waitUntil()
is only useful if you have some asynchronous task that you want to keep running after returning a response to the visitor. Usually the Workers runtime will cancel any background tasks spawned from a particular FetchEvent once it detects that the response has been sent. event.waitUntil()
tells the runtime to wait for another promise in addition to the visitor response before canceling any tasks. This probably doesn’t apply to your immediate needs. Further, event.waitUntil()
doesn’t return anything, so treating its return value as anything other than undefined
will lead to an exception.
Harris