프론트엔드/자바스크립트

Callback 지옥

테오구 2021. 8. 28. 16:39
728x90

1. 동기와 비동기

  • Javascript는 synchronous(동기적)이다
  • hoisting이 된 이후부터, 코드가 작성한 순서대로 실행됨
  • hoisting: var, function 선언 들이 코드 맨 위로 자동으로 올라가는 것
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 callback`), 2000);

출력 결과

 

✨위의 코드는 당연한거지만 기본중에 기본이니 확실히 알고가자!

2. 콜백 체인의 문제점

  • 가독성이 안좋다
  • 비즈니스 로직을 이해하기 힘듦
  • 디버깅 할 때, 굉장히 어려움
  • 유지보수 안좋음
class UserStorage {
  loginUser(id, password, onSuccess, onError) {
    setTimeout(() => {
      if (
        (id === `ellie` && password === `123`) ||
        (id === `gunwoo` && password === `1104`)
      ) {
        onSuccess(id);
      } else {
        onError(new Error(`not found`));
      }
    }, 2000);
  }

  getRoles(user, onSuccess, onError) {
    setTimeout(() => {
      if (user === `ellie`) {
        onSuccess({ name: `ellie`, role: `admin` });
      } else {
        onError(new Error(`no access`));
      }
    }, 1000);
  }
}


const id = prompt(`enter your id`);
const password = prompt(`enter your password`);

const userStorage = new UserStorage();
userStorage.loginUser(
  id,
  password,
  user => {
    userStorage.getRoles(
      user,
      userInfo => {
        alert(`name: ${userInfo.name}, role: ${userInfo.name}`);
      },
      error => {
        console.log(error);
      }
    );
  },
  error => {
    console.log(error);
  }
);

 

 

 

728x90

'프론트엔드 > 자바스크립트' 카테고리의 다른 글

원시 자료형(primitive)vs참조 자료형(referense)  (0) 2021.09.08
Promise  (0) 2021.08.28
JSON  (0) 2021.08.28
Array(2)의 사본  (0) 2021.08.28
Array(1)  (0) 2021.08.28