function grabJSON(url){
return new Promise(function(successFN, errorFN){
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'json';
processing(); //trigger special notification
request.onload = function(e){
successFN(request.response);
removeProcessing(); //hide special notification after 'onload'
};
request.onerror = function(){
debugger; //does not execute when URL is incorrect
errorFN(new Error('Failed to load from: ' + url));
};
request.send();
});
}
However, when I use eventListener - it fails to handle errors:
var previous = document.getElementById('prev');
previous.addEventListener('click', function(){
grabJSON('http://marsweather.ingenology.com/v1/archive/1').then(function(response){
debugger; //this works
console.log(response);
}).catch(function(err){
debugger; //this does not work. Why?
console.log('error occurred ', err);
});
});
Why is the catch block not working within the eventListener?
UPDATE: Dmitriy Loskutov recommended this - When should XMLHttpRequest's onerror handler fire