Forked from gauntface/gist:3415a0aeea34806a837a
Last active
August 29, 2015 14:17
Revisions
-
jakearchibald revised this gist
Mar 24, 2015 . 1 changed file with 12 additions and 39 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,40 +1,13 @@ function assertOkResponse(response) { if (response.type === 'opaque' || response.status === 200) { return response; } throw Error("Bad response: " + response.status); } fetch(url, {'mode': 'cors'}).then(assertOkResponse).then(function(response) { // cache }).catch(function(err) { console.error('Fetch Error :-S', err); throw err; }); -
jakearchibald renamed this gist
Mar 24, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Matthew Gaunt created this gist
Mar 24, 2015 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ var url = '/scripts/main.min.c18e3e78.js'; var opaqueResponsePromise = function(response) { console.log('opaque repsonse', response); // Cache opaque response }; var basicResponsePromise = function(response) { console.log('basic repsonse'); // Parse the response if(response.status !== 200) { throw new Error('Bad status code'); } return response.text() .then(function(text) { console.log('Response.text is complete'); }) .catch(function(err) { console.log('Oops = ', err); }); }; fetch(url, {'mode': 'cors'}) .then(function(response) { if (response.type === 'opaque') { console.log('The Response is opaque so we can\'t examine it'); // Do something with the response (i.e. cache it for offline support) return opaqueResponsePromise(response); } return basicResponsePromise(response); }) .catch(function(err) { console.error('Fetch Error :-S', err); });