map 함수 내에서 에러가 throw 된 경우 catch로 넘어가지 않는 문제
try {
items.map(i => {
await this.itemService.create(i);
}),
} catch(error) {
// throw된 error가 잡히지 않음
throw new BadRequestException(error.message);
}
// ItemService.js
const product = await this.productService.findByCode(i.product_code);
if (!product) {
throw new BadRequestException('PRODUCT_NOT_EXIST');
}
Promise.all 사용try {
await Promise.all(
items.map(async (i) => {
await this.itemService.create(i);
}),
);
} catch(error) {
// throw된 error가 잡힘
throw new BadRequestException(error.message);
}
map, reduce 에서 await 함수를 실행하는 방법map과 reduce에서의 해결 방안이 각각 달랐다.