Created
June 7, 2017 06:06
-
-
Save Axighi/717727652fb2399e5461f990de20e1cd to your computer and use it in GitHub Desktop.
This file contains 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
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