
Blocking: 하나의 작업이 끝날 때까지, 이어지는 작업을 “막는 것”
동기적(synchronous): 새로운 작업 시작 지점과 이전 작업 완료 시점이 같은 상황
ex): 카페에서 첫번째로 주문한 커피가 나오고 나서야 두번째로 커피를 주문할 수 있는것
Node.Js 개발자는 Node.js를 non-blocking하고 비동기적으로 작동하는 런타임으로 개발함
JavaScript에서 특히 비동기적 실행이라는 개념은 웹 개발에서 유용한데, 아래같은 작업들은 비동기적으로 작동되어야 효율적이기 때문



클라이언트:”서버로 접속하는 컴퓨터” -> 보통 우리들의 컴퓨터
서버: "무언가(서비스,리소스 따위)를 제공하는 컴퓨터" -> 웹 서버, 게임 서버 등

const printString = (string, callback) => {
setTimeout(
() => {
console.log(string)
callback()
},
Math.floor(Math.random() * 100) + 1
)
}
const printAll = () => {
printString("A", () => {
printString("B", () => {
printString("c", () => {})
})
})
}
printAll() // A,B,C 가 순서대로 출력됨 (순서가 안바뀜)
콜백함수의 문제점: 아래처럼 콜백함수 헬이 일어날 수 있음 -> promise 사용이 필요

1.프로미스의 3가지 상태(states)
1.Pending(대기) : 이행하지도 실패하지도 않은 상태로 비동기 처리가 아직 완료되지 않은 상태
2.Fulfilled(이행) : 비동기 처리가 완료되어 promise가 결과 값을 반환해준 상태
3.Rejected(실패) : 비동기 처리가 실패하거나 오류가 발생한 상태
new Promise(); //new Promise() 메서드를 호출 ->Pending(대기) 상태
new Promise(function(resolve, reject) {
// ...
}); //new Promise() 메서드를 호출할 때 콜백 함수를 선언가능, 콜백 함수의 인자는 resolve, reject
new Promise(function(resolve, reject) {
resolve();
}); // resolve를 호출 -> Fulfilled(이행) 상태
new Promise(function(resolve, reject) {
reject();
}); // reject를 호출 -> Rejected(실패) 상태
const printString = (string) => {
return new Promise((resolve, reject) => {
setTimeout(
() => {
console.log(string)
resolve()
},
Math.floor(Math.random() * 100) + 1
)
})
}
const printAll = () => {
printString("A")
.then(() => {
return printString("B")
printString("c", () => {})
})
.then(() => {
return printString("c")
})
}
printAll()
function gotoCodestates(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('1. go to codestates ')}, 100)
})
}
function sitAndCode(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('2. sit and code')}, 100)
})
}
function eatLunch(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('3. eat lunch')}, 100)
})
}
function goToBed(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('4. go to bed')}, 100)
})
}
gotoCodestates()
.then(date => {
console.log(data)
sitAndCode()
.then(data => {
console.log(data)
eatLunch()
.then(data => {
console.log(data)
goToBed()
.then(data => {
console.log(data)
})
})
})
})
gotoCodestates()
.then(date => {
console.log(data)
return sitAndCode()
})
.then(data => {
console.log(data)
return eatLunch()
})
.then(data => {
console.log(data)
return goToBed()
})
.then(data => {
console.log(data)
})
function gotoCodestates(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('1. go to codestates ')}, 100)
})
}
function sitAndCode(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('2. sit and code')}, 100)
})
}
function eatLunch(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('3. eat lunch')}, 100)
})
}
function goToBed(){
return new Promise((resolve, reject) => {
setTimeout (()=>{resolve('4. go to bed')}, 100)
})
}
const result = async () => {
const one = await gotoCodestates();
console.log(one)
const two = await sitAndCode();
console.log(two)
const three = await eatLunch();
console.log(three)
const four = await goToBed();
console.log(four)
}
result();
```)