[Javascript] 콜백 지옥과 프라미스 체이닝

박성찬·2021년 5월 30일
0

1. 콜백지옥

var callBack = function(text, done) {
  console.log('Log: ', text);
  done(text);
}

callBack('부팅 시작', function(done) {
  callBack('네트워크 설정 중..', function(done) {
    callBack('유저 프로필 설정 중..', function(done) {
      callBack('반갑습니다.', function(done) {
        console.log('마지막 로그: ', done);
      }) 
    })  
  })
})

2. 프라미스 체이닝

var callBack = function(text) {
  return new Promise(function(resolve, reject) {
    console.log('Log: ', text);
    resolve(text);
  })
}

callBack('부팅 시작').then(function(result) {
  return callBack('네트워크 설정 중..');
}).then(function(result) {
  return callBack('유저 프로필 설정 중..');
}).then(function(result) {
  return callBack('반갑습니다');
}).then(function(result) {
  console.log('마지막 로그:', result);
}).catch(function(error) {
  console.log(error);
})

참조: https://geundung.dev/53

profile
pptrollDev@gmail.com

0개의 댓글