Imagine getting this question during an interview.
Explain the difference between asynchronous and synchronous in Javascript.
Answer> Javascript as a single threaded language that can be non-blocking(자바스크립트는 비차단이 가능한 단일스레드 언어입니다.)
what is a program?
//these are global variables.
const a = 1;
const b = 10;
const c = 100;
With all memory we have limited amount, that we can actually have.
Memory leaks happen when you have unused memory.
Global variables are bad because there're unused memory.
console.log('1');
console.log('2');
console.log('3');
The call stack, if you remember, that's what it reads and executes our scripts.
let's imagine I have a function 'const one'
const one = () => {
const two =() => {
console.log('4')
}
two();
}
Let's revisit the statement from the beginning.
Answer> Javascript as a single threaded language that can be non-blocking(자바스크립트는 비차단이 가능한 단일스레드 언어입니다.)
- Single thread: it has only one call stack.
=> you can only do one thing at a time.- Call Stack is first in last out
=> Whatever's at the top of the call stack gets run first then below that, below that, below that.
Other language can have multiple call stacks which is called multithread. Multiple call stack might be beneficial, so that we don't keep waiting around for stuff.
Why was javascript designed to be single thread?
Single thread can be quite easy, since you don't have to deal with complicated scenarios that arise in multithreaded environment.
Synchronous programing: execute line1, line2.. in order.
you know the site, stackoverflow.
Stack Over Flow means that a stack is overflowing.
Overflow happens when the call stack just gets bigger and bigger until it just doesn't have enough space anymore.
How can we makes over flow?
function foo() {
foo()
}
It just keeps looping over and over, having recursion but there's no end in sight.
We keep adding foo to the call stack.
And we have a stack overflow.
- Javascript engine(v-8 engine) and Chrome has a memory heap and a call stack.
- Javascript is single thread, only one statement is executed at a time.
console.log('1');
...
massive work : reallllly massive work.
...
console.log('3'); // take a long time to get logged.
The website would freeze until the task is done and the user just wait there.
Synchronous task: if we have one function that takes up a lot of time, it's going to hold up.
We need "nonblocking".
Javascript is a single threaded language that can be non-blocking.
Synchronous execution is great because it's predictable but it can get slow in tasks like image processing, making requests over the network like API calls.
We need something more that just synchronous tasks.
console.log('1');
setTimeout(() => {
console.log('2');
}, 2000) // first parameter is the function that we want to run
// second parameter is how many milli seconds we want to wait.
console.log('3');
'console.log(2)' is done 2 seconds later.
//CALL STACK
//WEB API
//CALLBACK QUEUE
//EVENT LOOP
console.log('1');
setTimeout(() => {
console.log('2');
}, 0)
console.log('3');
execute consolelog3 first than 2
Let's recap.
If you wanted to load your latest tweets onto a web page and you do this synchronously, then visitors to your website won't be able to do anything until those tweets are loaded.
This could cause a long delay they even get to see the content of his site.
They may not be able to click anywhere and the page will feel like it's frozen.
element.addEventListener('click', () => {
console.log('click')
})
Every time a click happens on the web page, on the DOM we run the callback function, which is console.log('click').
when you try and talk between machines like speaking to a database making network requests, image processing, reading files...