I signed for the JS fundamentals course made by Flavio Copes. Every day, Flavio sends emails to participants with a couple of questions/tasks. There should be 35 email letters in total. It’s not obligatory to answer them, but answering questions is the important part of the course. So I decided to share my answers with the world: in the best case, it can help anybody besides me, in the worst case it just helps me.
Describe me what is a breakpoint and how is it useful when debugging
A breakpoint is an instruction for the interpreter to stop JS execution on the specified line of code. Developers can add breakpoints whenever and as many as they want to.
Breakpoints are useful because they allow to catch unexpected behavior in a program. Breakpoints also come in handy when we want to track how variables change during the program execution.
What is an exception? why would we want to throw an exception?
An exception is a way to handle errors. Any program stopped when an error occurs, and it might be inappropriate to stop the execution. To prevent a program from being halted, there is a possibility to handle errors vie the exception paradigm.
Write me a practical example of using setInterval(), and clearing the interval when something happens
We can create a simple timer set for 60 seconds which will call alert() when the time will be up:
Do you think there is a problem in having too many callbacks nested one into another?
Having too many callbacks in a program means a reduced level of readability, which is definitely a problem.
Another point is that callbacks create deferred computations which can be memory consuming in case of multiple nested callbacks.
Which are the 3 states of a promise?
- pending — the caller function has just been called
- resolved — the promise has successfully returned a value (a resolve function has been executed)
- rejected — the promise hasn’t returned an expected value (an error occurred, i.e. a reject function has been executed)
What does it mean that a promise has resolved?
It means that the resolve function (one of a promise’s callbacks) has been successfully executed and returned a value.
How can we wait until multiple promises are resolved?
This is when we can use nested/chaining promises. In general, it looks like this:
What is the purpose of the `finally()` method of a promise?
We use it when we need a promise to do something whatever its condition will be: resolved or rejected. The finally() method will definitely run, no matter what :)