Skip to content

Instantly share code, notes, and snippets.

@unarist
Created June 15, 2017 17:02
Show Gist options
  • Save unarist/d21635da3487753bf15dab986aed994e to your computer and use it in GitHub Desktop.
Save unarist/d21635da3487753bf15dab986aed994e to your computer and use it in GitHub Desktop.
window.fetch = window.fetch || function (url, options) {
return new Promise(function (resolve) {
const xhr = new XMLHttpRequest();
xhr.onloadend = function() {
resolve(Object.assign({}, xhr, {
ok: xhr.status === 200,
status: xhr.status,
statusText: xhr.statusText
}));
};
xhr.open(options.method || 'get', url);
for (const key in options.headers)
xhr.setRequestHeader(key, options.headers[key]);
options.body && xhr.send(options.body);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment