피자 주문 문제
이 과정을
그전의 전통적인 callback
chooseToppings(function(toppings) {
placeOrder(toppings, function(order) {
collectOrder(order, function(pizza) {
eatPizza(pizza);
}, failureCallback);
}, failureCallback);
}, failureCallback);
promise
chooseToppings()
.then(function(toppings) {
return placeOrder(toppings);
})
.then(function(order) {
return collectOrder(order);
})
.then(function(pizza) {
eatPizza(pizza);
})
.catch(failureCallback);
=> 겹치는게 아니라 한줄로 줄을 설 수 있다.
=> catch는 꿀 빨고 있는게 아니라
=> 언제든지 내려오다가 실패하면 작동된다.
정리하자면
화살표 함수를 쓰면 더 간결해짐
chooseToppings()
.then(toppings =>
placeOrder(toppings)
)
.then(order =>
collectOrder(order)
)
.then(pizza =>
eatPizza(pizza)
)
.catch(failureCallback);
chooseToppings()
.then(toppings => placeOrder(toppings))
.then(order => collectOrder(order))
.then(pizza => eatPizza(pizza))
.catch(failureCallback);
chooseToppings().then(placeOrder).then(collectOrder).then(eatPizza).catch(failureCallback);
=> 심지어 이렇게까지 할 수 있다.
=> 2번은 지금 뭔 소리인지 모르겠다....