Using a simple service-worker code, I try to save and defer failed requests.
To do this, I have to identificate failed requests first but I don't understand why the way I do it always consider the requests as successful even if they are not.
Here is the code I wrote:
function fromNetwork (request) {
return new Promise(function (fulfill, reject) {
fetch(request).then(function (response) {
console.log('request succeed')
fulfill(response)
}, reject)
})
}
self.addEventListener('fetch', (event) => {
event.respondWith(
fromNetwork(event.request)
.catch(function () {
console.log('request failed')
return Promise.reject('Request failed')
})
)
})
And here is a screenshot of the output of the console:
What am I doing wrong ?