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 | 31 |
Tags
- 힙
- 그리디
- level3
- 다익스트라
- 백준
- 구현
- BFS
- 딕셔너리
- 다이나믹 프로그래밍
- 스택
- N과M
- 코딩테스트
- dfs
- 프로그래머스
- 수학
- 가상메모리
- 재귀
- MYSQL
- 브루트포스
- dict
- 가상메모리 관리
- programmers
- level1
- level0
- 파이썬
- python
- BOJ
- 운영체제
- level2
- DP
Archives
- Today
- Total
동캄의 코딩도장
JS [callback-to-promise 예제] 본문
// Callback kHell example
class userStorage{
loginUser(id,password){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
if(
(id==='ellie' && password ==='dream') ||
(id ==='coder' && password ==='academy')
){
resolve(id);
}
else{
reject(new Error('not found'));
}
},2000);
});
}
getRoles(user){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
if(user==='ellie'){
resolve({name:'ellie',role:'admin'});
}
else{
reject(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)
.then(userstorage.getRoles)
.then(user=>alert(`Hello ${user.name}, you have a ${user.role} role`))
.catch(console.log);
'front > HTML&CSS&JS' 카테고리의 다른 글
JS [콘솔 사용 tip] (0) | 2022.01.27 |
---|---|
JS [async] (0) | 2022.01.25 |
JS [promise] (0) | 2022.01.25 |
JS [call back] (0) | 2022.01.25 |
JS [JSON] (0) | 2022.01.25 |