반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 에라스토테네스의 체
- 가상메모리
- level1
- level3
- 수학
- 구현
- 다이나믹 프로그래밍
- BOJ
- 딕셔너리
- 운영체제
- 힙
- 파이썬
- level2
- 브루트포스
- programmers
- 코딩테스트
- 재귀
- BFS
- 프로그래머스
- 스택
- 다익스트라
- 그리디
- python
- 가상메모리 관리
- N과M
- MYSQL
- level0
- 백준
- dfs
- DP
Archives
- Today
- Total
동캄의 코딩도장
JS [call back] 본문
반응형
'use strict';
// JavaScript is synchronous.
// Excute the coe block by order after hoisting.
// hoisting: var, function declaration
setTimeout(()=> console.log('2'),1000)
// 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 kHell 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');
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);});
반응형
'front > HTML&CSS&JS' 카테고리의 다른 글
JS [callback-to-promise 예제] (0) | 2022.01.25 |
---|---|
JS [promise] (0) | 2022.01.25 |
JS [JSON] (0) | 2022.01.25 |
JS [array API] (0) | 2022.01.24 |
JS [array] (0) | 2022.01.24 |