Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect promise logic #38

Open
mariuszdev opened this issue Dec 6, 2018 · 1 comment
Open

Incorrect promise logic #38

mariuszdev opened this issue Dec 6, 2018 · 1 comment

Comments

@mariuszdev
Copy link

mariuszdev commented Dec 6, 2018

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.

@DavidScales
Copy link
Collaborator

Sorry for the slow response. Yeah that doesn't look right to me, we are currently in a bit of publishing limbo but I will leave this open so I can make a fix once publishing is sorted. Thanks for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants