fetchres
Some useful response handlers for using Fetch API in the real world.
Warning: doesn't come with Fetch or Promise, so you'll need to bring your own polyfill (unless you're using this module within Service Workers, where both fetch
and Promise
are already defined).
Installation
npm install --save fetchres
Or
bower install --save fetchres
Usage (node)
var fetchres = require('fetchres');
fetch('https://mattandre.ws/my-json-endpoint')
.then(fetchres.json)
.then(function(data) {
console.log(data);
})
.catch(function(err) {
if (err.name === fetchres.BadServerResponseError.name) {
console.error(err);
} else if (err.name === fetchres.InvalidJsonError.name) {
console.error('malformed json returned');
} else {
debugger;
}
});