Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unrejectable fetch
import fetch from 'isomorphic-fetch';
export default function fetcher(url, options = {}) {
return new Promise((resolve) => {
fetch(url, Object.assign({}, {
credentials: 'same-origin',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, options))
.then(response => {
const status = +response.status;
response.json()
.then(data => resolve({ status, data }))
.catch(error => resolve({ status, error }));
})
.catch(error => resolve({ error }));
});
}
@LopatkinEvgeniy

This comment has been minimized.

Copy link
Owner Author

LopatkinEvgeniy commented Sep 30, 2016

Usage inside generator:

const { status, data, error } = yield fetcher('/some/url');

if (error) {
  console.log('error', error);
  return;
}

console.log('data', data)
@LopatkinEvgeniy

This comment has been minimized.

Copy link
Owner Author

LopatkinEvgeniy commented Sep 30, 2016

Go-style error handling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.