I would like to create my own Event Handler. For good example, XMLHttpRequest, this has onreadystatechange method which can fetch the status events just like below coding.
I want to have similar function, but I could not find it out (I tried AddEventListener/Promise etc). Does anyone know how to implement this?
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
switch (xhr.readyState) {
case 0:
//not initialized
break;
case 1:
//loading
break;
case 2:
//waiting for response
break;
case 3:
//receiving data
break;
case 4:
if (xhr.status == 200){
// Do something
}
break;
}
}