티스토리 뷰
- JavaScript is synchronous
- Execute the code block in order after hoisting
- hoisting : var, function declaration
console.log('1');
setTimeout(() => console.log('2'), 1000);
console.log('3');
// 1, 3, 2
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);
Callback Hell example
class UserStorage {
loginUser(id, password, onSuccess, onError) {
setTimeout(() => {
if (
(id === 'ellie' && password === 'dream') ||
(id === 'coder' && password === 'academy')
) {
onSuccess(id);
} else {
onError(new Error('not found'));
}
}, 2000);
}
getRoles(user, onSuccess, onError) {
setTimeout(() => {
if (user === 'ellie') {
onSuccess({ name: 'ellie', 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');
userStroage.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)});
※출처
'JavaScript & TypeScript' 카테고리의 다른 글
React - 시작하기 (0) | 2020.11.23 |
---|---|
11. Promise (0) | 2020.11.15 |
9. JSON (0) | 2020.11.15 |
8. Array APIs (0) | 2020.11.14 |
7. Array, API (0) | 2020.11.14 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Spring Security
- 이펙티브자바 아이템59
- HTTP 완벽가이드
- 모던자바스크립트
- 프로그래머스
- ㅇㄷㅇㅈ
- BOJ
- 드림코딩
- 프로그래머스 SQL
- JPA 연관관계 매핑
- 패스트캠퍼스 컴퓨터공학 완주반
- 이펙티브자바
- 김영한 JPA
- 백기선 스터디
- HTTP 완벽 가이드
- JS 딥다이브
- 백준
- 킹수빈닷컴
- js promise
- 이펙티브자바 아이템60
- js array
- GCP
- REST API
- dreamcoding
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 김영한 http
- js api
- 이펙티브자바 스터디
- java
- http
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함