In an effort to remove the use of jQuery from my code, I tried to replace the $.Deferred();
by new Promise()
.
I noticed the usage are slightly different, and I'm still learning how it works.
Here is a simplified extract from my code:
function do_match (resolve, reject) {
fetch( /* ... */).then (async function(response) {
/* do some suff */
document.getElementById("match").insertAdjacentHTML('beforeend', '<div class="player"></div>');
resolve("done");
});
}
function do_myMarket () {
var elements = document.querySelectorAll('.player');
//here elements is sometimes null...
}
p1 = new Promise(do_match);
p1.then(do_myMarket, null);
While I would have expect do_myMarket
to only be called after the promise is resolved, if the fetch is not fast enough, do_myMarket
can be called before the elements are available in the page.
Putting breakpoints if elements
is null and resolve()
confirmed me this behavior.
Am I missing something? Why would this happen?