Seven years later I came across my old post and decided to try and figure it out myself. It is possible to embed the window
event listener; the relevant line of code is at the bottom of the function and works fine in Waterfox and Chrome.
function ajax_promise(method,url)
{
var xhr = new XMLHttpRequest();
return new Promise(function (resolve, reject)
{
xhr.onabort = function()
{
alert('xhr aborted!');
}
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200) {resolve(xhr);}
else {reject({status: xhr.status, statusText: xhr.statusText});}
}
};
window.onkeydown = function(event) {if (event.key && event.key == 'Escape') {xhr.abort();}}
xhr.open(method, url, true);
xhr.send();
});
}