์ฝ๋ฐฑ์ง์ฅ
// ๊ธฐ๋ณธ์ธํ
class UserStorage {
loginUser(id, password, onSuccess, onError) {
setTimeout(() => {
if (
(id === 'master' && password === 'master00') ||
(id === 'submaster' && password === 'submaster00')
) {
onSuccess(id);
} else {
onError(new Error('not found'));
}
}, 2000);
}
getRoles(user, onSucces, onError) {
setTimeout(() => {
if (user === 'master') {
onSuccess({name: 'Master', role : 'admin'});
} else {
onError(new Error('Go away!'));
}
}, 1000);
}
}
์ข์ง ์์ ์ฝ๋ฐฑ์ง์ฅ ์์
(id์ password๋ฅผ ๊ฒ์ฌํด์ id๊ฐ master์ผ ๊ฒฝ์ฐ admin role์ ๋ถ์ฌํ๋ ์ฝ๋์ด๋ค.)
//์ฝ๋ฐฑ์ง์ฅ
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);
}
);
๋ฌธ์ ์
- ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค. (logic์ ์์๋ณด๊ธฐ ํ๋ค๋ค)
- ์๋ฌ๊ฐ ๋ฐ์ํ์ ์ ๋๋ฒ๊น ํ๊ธฐ ์ด๋ ต๋ค.
์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko/