Skip to content

Instantly share code, notes, and snippets.

@heleneshaikh
Last active October 29, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heleneshaikh/68835dd7d4d27485080ae10661493ec0 to your computer and use it in GitHub Desktop.
Save heleneshaikh/68835dd7d4d27485080ae10661493ec0 to your computer and use it in GitHub Desktop.
Fetch with polyfill and error wrapper
import 'whatwg-fetch'; //polyfill
window.fetch(Routing.generate('booking_data'), { //window.fetch -> polyfill
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
checkin: event.start.toDateString(),
checkout: addDays(event.start, 7).toDateString()
})
})
.then(checkStatus)
.then(response => response.json())
.catch(error => console.log(error));
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
let error = new Error(response.statusText);
error.response = response;
throw error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment