I'm trying to promisify the native XHR,
now here's the problem, when I use the following code:
function request(method, url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = resolve;
xhr.onerror = reject;
xhr.send();
});
}
it returns a promise instead of a XHR object,
so I cant use something like xhr.abort()
in this way:
xhr = request('GET', 'http://google.com').then(function (e)
{
// ...code...
}, function (e)
{
// ...code...
});
// When user press the stop button.
xhr.abort();
Is there anyway to make it returns a XHR object and still keep it promisable?
edit: This is not asking how to promisify a XHR object but how to make a "promisified" XHR object return the XHR object instead of the Promise.