Javascript Flashcards
Async Evolution
callbacks (unmanageable callback hell) → promises with then and catch → async/await (write code normally, no long promise chaining)
Callback
A function that is passed as an argument. The function will be executed after a certain operation is finished. This is useful for handling async operations.
Promise
An alternative approach for callbacks for handling async operations. It allows you to set an operation to run after another operation finish. It is a syntactic improvement that prevents unclean callback hell.
a promise returns resolve or reject
handled with then and catch
async / await keyword
Syntactic sugar for handling promise, without then and catch
const res = await someFunc() console.log(res)
make sure res is printed only after someFunc resolved
Basic of async
Blocking codes: expensive task or intensive chunk of code
When executed without returning control to the browser, it will appear frozen
Javascript main thread is single-threaded
A thread can only execute a task at a time
expensive operations: I/O operations like HTTP requests, disk reads and writes or database operations
when encountering an async I/O operations it will be assigned to a kernel thread (system kernel are multi-threaded)
with promise or callback, the I/O operation will not block the main thread
after operation is finished, callback is being put into a callback queue in the event loop
once it can be safely handled it will be pushed to the JS call stack, to be called by the main thread
for (let i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 1000 ); }
for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 1000 ); }
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
What is 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 is Capturing
event goes down to the element.
event target vs current target
-
event.target
– is the “target” element that initiated the event, it doesn’t change through the bubbling process. -
this
– is the “current” element, the one that has a currently running handler on it.
How to stop Bubbling
event.stopPropagation()
Capturing and Bubbling Phase
- Capturing phase – the event goes down to the element.
- Target phase – the event reached the target element.
- Bubbling phase – the event bubbles up from the element
Arrow Function vs Regular Function
- Structure
- This
- Implicit Returns
If the arrow function contains one expression, without curly braces, then the expression is implicitly returned. These are the inline arrows function. - Constructor
can’t be used as constructor - Argument
arguments object equals to the arguments of the outer function, can use …args to get direct arguments
Arrow vs Regular “this”
> `this`value inside of an arrow function always equals`this` value from the outer function. In other words, the arrow function resolves`this` lexically. > no more`const self = this` or`callback.bind(this)`workarounds.
Debounce vs Throttle
Debouncing forces a function to wait a certain amount of time before running again. The debounced function will ignore all calls to it until the calls have stopped for a specified time period. Only then will it call the original function. (e.g. autosave feature)
To throttle a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds) (e.g. limiting API hit)
What is a Closure
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.