Decoding the code
Talk about something you learned this week.
JavaScript is a naturally asynchronous language… this can be very helpful until it isn’t. It turns out that the GeoLocation API in particular can take a very long time to check a location. Other API calls may also take a long time in the same way. When using fetch with promises and then using other functions to do something with this data this can certainly cause problems. The solution? More promises! The promise constructor can save you here and it is a wonderful thing to figure out. Simply wrap each function like so, put the code you need, then resolveand then use them with the .then
keyword as needed:
const functionOne = () => { return new Promise((resolve)=>{ console.log('I do stuff') resolve() })}const functionTwo = () => { return new Promise((resolve)=>{ console.log('I too do stuff') resolve() })}
Explain Function.prototype.bind()
The bind()
method creates a new function that, when called, has its this
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
Describe event bubbling.
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
What’s the difference between the window load event and document DOMContentLoaded event?
The DOMContentLoaded event fires when parsing of the current page is complete; the load event fires when all files have finished loading from all resources, including ads and images.
Describe the call stack.
The callstack is how the JavaScript engine keeps track of the order to run events in. As I discovered this week, this can get trick especially when dealing with fetch requests that introduce an asynchronous nature to the code. Basically, when a function is called it is added to the call stack and the JS engine starts to execute. Any functions called by the function are then added to the call stack. Once a function is completed it is removed from the call stack and the engine moves to the next item. Beware: if the stack takes up too much space this can result in “stack overflow”.