const getHen = () =>
    new Promise((resolve, reject) => {
        setTimeout(() => resolve('chickin'), 1000);
}); // return new Promise
const getEgg = (hen) => 
    new Promise((resolve, reject) => {
				// 정상 처리시
        //setTimeout(() => resolve(`${hen} => egg`), 1000);

				// 에러 발생시
        setTimeout(() => reject(new Error(`error! ${hen} => egg`)), 1000);
}); // return new Promise
const cook = (egg) => 
    new Promise((resolve, reject) => {
        setTimeout(() => resolve(`${egg} => phlahe`), 1000);
}); // return new Promise

getHen()
    .then(hen => getEgg(hen)) // getEgg() -> Promise 객체
    .then(egg => cook(egg))   // cook()   -> Promise 객체
    .then(meal => console.log(meal));

// callback 함수 특징
// callback 함수에서 하나의 파라미터를, return 함수로 전달 할 때 코드를 간략히 할 수 있다.
getHen()
    .then(getEgg)
    .then(cook)
    .then(console.log);

getHen()
		.then(getEgg)
		.catch(err => {
		    return '@@@';    // 에러가 발생 하면 return 으로 값을 대체하여 넘기기
})
		.then(cook)
		.then(console.log)
		.catch(console.log); // 중간에 에러가 발생하면 catch에서 발생한 에러를 잡는다.

// 정상 처리시 출력
// chickin => egg => phlahe
// chickin => egg => phlahe
// chickin => egg => phlahe

// 에러 발생시 출력
// Uncaught (in promise) Error: error! chickin => egg
//    at promise.js:116
// Uncaught (in promise) Error: error! chickin => egg
//    at promise.js:116
// @@@ => phlahe

getHen()의 return 값은 Promise 이다. 그러므로 .then() 이 가능하다.

then의 callback 함수 onfulfilled 의 파라미터 value는 resolve 의 value 를 받아 / return 값으로 any 나 Promise 객체를 반환한다.

then은 최종적으로 Promise 객체를 반환한다.