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