
const f1 = (callback) => {
    setTimeout(() => {
        console.log(“1번째 주문이 완료되었습니다.”);
        callback()
    }, 1000)
}
const f2 = (callback) => {
    setTimeout(() => {
        console.log(“2번째 주문이 완료되었습니다.”);
        callback()
    }, 3000)
}
const f3 = (callback) => {
    setTimeout(() => {
        console.log(“3번째 주문이 완료되었습니다.”);
    }, 2000)
}
f1(function(){
    f2(function(){
        f3(function(){
            console.log(“끝”)
        })
    })
})
const f1 = () => {
    return new Promise((res, rej) => {
        setTimeout(() => {
            res(“1번 주문 완료”);
        }, 1000);
    });
};
const f2 = (message) => {
    console.log(message)
    return new Promise((res, rej) => {
        setTimeout(() => {
            res(“2번 주문 완료”);
        }, 3000);
    });
};
const f3 = (message) => {
    new Promise((res, rej) => {
        setTimeout(() => {
           res(“3번 주문 완료”);
        }, 2000);
    });
};
func1()
    .then((res) => func2(res))
    .then((res) => func3(res))
    .then((res) => console.log(res))
// 결과
(1초 후) 1번 주문 완료
(3초 후) 2번 주문 완료
(2초 후) 3번 주문 완료
then은 Promise 객체가 반환하는 result 프로퍼티의 값(value)을 결과로 전달받아 다음 동작을 실행시킨다..then을 통해 함수를 호출하고 여기에 res 즉 result값을 인수로 받는 함수 func2()를 호출한다.func2()가 호출 된 이후 시점이며, 때문에 마지막 함수 호출 시 console.log()로 “3번 주문 완료”를 찍어주어야 한다..catch() 메서드를 사용하여 체인 전체에서 발생하는 에러를 한 곳에서 처리할 수 있습니다.출처 : 마이크로소프트 코파일럿
// catch와 finally까지 사용함
const f1 = () => {
     return new Promise((res, rej) => {
         setTimeout(() => {
             res(“1번 주문이 완료되었습니다.”);
         }, 1000);
     });
};
const f2 = (message) => {
    console.log(message);
    return new Promise((res, rej) => {
        setTimeout(() => {
             res(“2번 주문이 완료되었습니다.”);
         }, 3000);
    });
};
const f3 = (message) => {
    console.log(message);
    return new Promise((res, rej) => {
        setTimeout(() => {
             res(“3번 주문이 완료되었습니다.”);
        }, 2000);
    });
};
console.log(“시작”)
f1()
    .then((res) => f2(res))
    .then((res) => f3(res))
    .then((res) => console.log(res))
    .catch(console.log)
    .finally(() => {
        console.log(“끝..”);
        }
    );
=> 두 번째 프로미스가 reject를 반환하도록 코드를 수정
const f1 = () => {
     return new Promise((res, rej) => {
         setTimeout(() => {
             res(“1번 주문이 완료되었습니다.”);
         }, 1000);
     });
};
const f2 = (message) => {
    console.log(message);
    return new Promise((res, rej) => {
        setTimeout(() => {
             rej(“xxx”);    // 이 부분이 reject로 수정됨
         }, 3000);
    });
};
const f3 = (message) => {
    console.log(message);
    return new Promise((res, rej) => {
        setTimeout(() => {
             res(“3번 주문이 완료되었습니다.”);  
        }, 2000);
    });
};
console.log(“시작”)
f1()
    .then((res) => f2(res))
    .then((res) => f3(res))
    .then((res) => console.log(res))
    .catch(console.log)
    .finally(() => {
        console.log(“끝..”);
        }
    )
// 결과
시작
(1초 후)1번 주문 완료
(3초 후)xxx
끝
reject를 반환하면 그 다음 프로미스를 실행하지 않으니 주의.// 선언 하는 방법은 같고 호출 시 다음과 같이 작성한다.
// res만 반환한다고 가정한다.
Promise.all([ f1, f2, f3 ]).then((res) => {
    console.log(res);
});
// 결과
undefined
undefined
(3초후)
[ ‘1번 주문이 완료되었습니다.‘, ‘2번 주문이 완료되었습니다.’, ‘3번 주문이 완료되었습니다.’ ]
Promise.all([])을 사용하면 동시에 프로미스를 처리하도록 하여 가장 오래걸리는 시간 3초에 모든 응답을 반환 한다.reject를 반환하면 아무것도 반환하지 않는다.Promise.all([])과 동일한데 all 키워드를race로만 바꿔주면 되고 역시 배열을 받는다.