Skip to content

Incorrect promise logic #38

@mariuszdev

Description

@mariuszdev

https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker#serving_files_from_the_cache

Cache then network example.

var networkDataReceived = false;

startSpinner();

// fetch fresh data
var networkUpdate = fetch('/data.json').then(function(response) {
  return response.json();
}).then(function(data) {
  networkDataReceived = true;
  updatePage(data);
});

// fetch cached data
caches.match('/data.json').then(function(response) {
  if (!response) throw Error("No data");
  return response.json();
}).then(function(data) {
  // don't overwrite newer network data
  if (!networkDataReceived) {
    updatePage(data);
  }
})
.catch(function() {
  // we didn't get cached data, the network is our last hope:
  return networkUpdate;
})
.catch(showErrorMessage) // never called, because previous catch swallows thrown error
.then(stopSpinner()); // stopSpinner called immediately, should be `then(stopSpinner)`.

See comments for last 2 lines of above snippet

If there is better place to report this problem, please let me know. I'll move it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions