javascript
callback
by ozero
2024. 8. 4.
'use strict';
//JavaScript is synchronous.
//hoisting: var, funciton declaration
console.log('1');
setTimeout(()=>console.log('2'), 1000);
console.log('3');
// Synchronous callback 즉각적으로 동기적으로 실행
function printImmediately(print){
print();
}
printImmediately(() => console.log('hello'));
// Asynchronous callback 언제 실행될지 모름
function printwithDelay(print, timeout){
setTimeout(print, timeout);
}
printwithDelay(()=> console.log('async callbak'), 2000);
//javascript는 함수를 인자로 호출할수 있음
// Callback Hell 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 fount'));
}
}, 2000);
}
getRoles(user, onSuccess, onError) {
}
set
}