Skip to content

Instantly share code, notes, and snippets.

@Axighi
Created June 7, 2017 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Axighi/717727652fb2399e5461f990de20e1cd to your computer and use it in GitHub Desktop.
Save Axighi/717727652fb2399e5461f990de20e1cd to your computer and use it in GitHub Desktop.
function futch(url, opts={}, onProgress) {
return new Promise( (res, rej)=>{
var xhr = new XMLHttpRequest();
xhr.open(opts.method || 'get', url);
for (var k in opts.headers||{})
xhr.setRequestHeader(k, opts.headers[k]);
xhr.onload = e => res(e.target.responseText);
xhr.onerror = rej;
if (xhr.upload && onProgress)
xhr.upload.onprogress = onProgress; // event.loaded / event.total * 100 ; //event.lengthComputable
xhr.send(opts.body);
});
}
futch('/').then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment