1

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:enter image description here

What am I doing wrong ?

Victor Castro
  • 1,232
  • 21
  • 40
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 30 '18 at 13:27
  • As you can see, you get `request succeed` - with a 500 status. It rejects only when you don't get a valid HTTP response at all. – Bergi Apr 30 '18 at 13:32

0 Answers0