3 ms·
Do you have an example of a DOM operation that is async ?
by simlevesque 5y ago
Do you have an example of a DOM operation that is async ?
- giantrobot 5y agoCreating a new element on the DOM, especially when it is creating a new script element. It takes non-zero time to load and parse but returns control to JavaScript immediately. So you need to wait on the element with like onLoad before calling a function in that script. There's some more instances I've forgotten. The situation is better now with promises but it was a pain in the ass just a few years ago.
- simlevesque 5y agoWell, yeah, some DOM elements have async side effect, but they are still added to the DOM synchronously. Creating the element and loading it's content are two completely separate things.
- giantrobot 5y agoThat's non-obvious to people from other languages, at least it was to me. The asynchronous nature of DOM manipulation also comes up when the DOM gets large and you naively try updating elements querying by tag or class. It's idiosyncrasies aren't deal breakers but they're non-obvious. They also crop up once you're into medium complexity code. At the level of simple/trivial code machines and JavaScript engines are fast enough to paper over naively written code. But you get to a certain level of complexity and you get bit by some colored function issue or something. It's annoying bringing something up where JavaScript is not the primary focus of the project. This is all anecdotal, there's plenty of people that might never have issues. But it was all stuff that tripped me up around 2009-10 having to write some JS after not having touched it since before you could make XHRs or manipulate the DOM directly.