동기란?
hoisting이란?
: var, function을 선언한 코드가 제일 위로 올라가게 되는 것
비동기란?
: 우리가 원하는 때에 다시 호출하는 함수
Synchronous callback VS Asynchronous callback
// Synchronous callback
function printImmediately(print) {
print();
}
printImmediately(() => console.log('hello'));
// Asynchronous callback
function printWithDelay(print, timeout) {
setTimeout(print, timeout);
}
printWithDelay(() => console.log('async callback'), 2000);
콜백 체인의 문제점
class UserStorage {
loginUser(id, password, onSuccess, onError) {
setTimeout(() => {
if {
(id === 'juli' && password === '123456') ||
(id === 'sm' && password === '2345')
) {
onSuccess(id);
} else {
onError(new Error('not found'));
}, 2000);
}
getRoles(user, onSuccess, onError) {
setTimeout(() => {
if (user === 'juli') {
onSuccess({ name: 'juli', role: 'admin' });
} else {
onError(new Error('no access'));
}
}, 1000);
}
}
const userStorage = new UserStorage();
const id = prompt('enter your id');
const password = prompt('enter your password');
userStorage.loginUser(
id,
password,
user => {
userStorage.getRoles(
user,
(userWithRole) => {
alert(`Hello ${userWithRole.name}, you have a ${userWithRole.role} role`);
},
error => {
console.log(error)
};
);
},
error => {
console.log(error)
}
);
https://www.youtube.com/watch?v=s1vpVCrT8f4&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=11