I have two bits of code which i would expect to have worked in the same way however they dont - only one actually works and i cant figure out why.
Working one
self.addEventListener('fetch',(e)=>{ e.respondWith(handleRequest(e.request)) }) async function handleRequest(req){ const res = await fetch(req); const cache = await caches.open(cacheName) if(res.ok){ // add the response to the caches and return the response await cache.put(req, res.clone()) return res; }else{ const res = cache.match(req) return res } } non working one
self.addEventListener('fetch',(e)=>{ e.respondWith(async () => { const res = await fetch(e.request); const cache = await caches.open(cacheName) if(res.ok){ // add the response to the caches and return the response await cache.put(e.request, res.clone()) return res; }else{ const res = cache.match(e.request) return res } }) }) Can anyone see why this is?
async arrow function passe.request` as a parameter as you did for normal function call.