When using this code:
document.addEventListener("keydown", function(e) {
if (e.keyCode == 9)
{
storage.get("items", function(obj) { // this is an asynchronous call
if (some_condition(obj)) {
e.preventDefault();
e.stopPropagation();
}
});
}
}
if some_condition
(depending on the result of the asynchronous call) is True, e.preventDefault();
and e.stopPropagation();
will be executed too late because storage.get
is asynchronous.
What option is possible to decide if preventDefault() should be called or not after the result of an asynchronous call?
I was thinking about this solution:
document.addEventListener("keydown", function(e) {
if (e.keyCode == 9)
{
e.preventDefault(); // always preventDefault and stopPropogation,
e.stopPropagation(); // we'll maybe restore it later
storage.get("items", function(obj) { // this is an asynchronous call
if (!some_condition) {
e.reenableDefault(); // contrary of preventDefault
e.restartPropagation(); // contrary of stopPropagation
}
});
}
}
but obviously there is no e.restartPropagation();
function!
Note: I already read How to reenable event.preventDefault? but this did not help in this context.