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
- BOJ
- 다익스트라
- N과M
- 다이나믹 프로그래밍
- 수학
- BFS
- MYSQL
- 재귀
- 가상메모리
- level2
- DP
- 힙
- 코딩테스트
- 백준
- 스택
- dfs
- dict
- 프로그래머스
- 구현
- programmers
- level0
- 그리디
- python
- 브루트포스
- 파이썬
- 딕셔너리
- 운영체제
- level3
- 가상메모리 관리
- level1
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 |