addEventListener()takes a callback (the "do something" function) and then calls it when
getData()is a function that fetches some data from a server and returns it as an object that we can use in our code:
pieceOfDataout of the returned data, the function
getData()will most likely still be fetching, so
myDatawill not be the expected data, but will be
You Don't Know JS. In Chapter 2, the author explains the problems with callbacks and why callback hell will be your worst enemy (hint: it's the inversion of control and non-linear nature of callbacks). In Chapter 3, you go deep into the how and why of promises. This chapter is not the easiest read, but you'll be a promise professional if you take the time to properly digest it. It's worth the effort.