Created
September 30, 2016 14:31
-
-
Save LopatkinEvgeniy/ecebe5c64ca9d25eadf9a9d3c51e7eaf to your computer and use it in GitHub Desktop.
Unrejectable fetch
This file contains hidden or 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 characters
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 })); | |
}); | |
} |
Go-style error handling
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage inside generator: