Skip to content

Commit

Permalink
Merge pull request #269 from boostcampwm2023/feat/be/promise
Browse files Browse the repository at this point in the history
์ƒํ’ˆ ์ •๋ณด ์กฐํšŒ promise ๋ฐฉ์‹ ๋ณ€๊ฒฝ
  • Loading branch information
sickbirdd authored May 8, 2024
2 parents 69a10ad + bb3ada8 commit 4901615
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions backend/src/cron/cron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ export class CronService {
@Cron('0 */10 * * * *')
async cyclicPriceChecker() {
const totalProducts = await this.productRepository.find();
const recentProductInfo = await Promise.all(
totalProducts.map(async ({ productCode, id, shop }) => {
const recentProductInfoPromises = totalProducts.map(async ({ productCode, id, shop }) => {
try {
const productInfo = await getProductInfo(shop, productCode);
return { ...productInfo, productId: id };
}),
);
} catch (error) {
return null;
}
});
const recentProductInfoResults = await Promise.allSettled(recentProductInfoPromises);
// ์„ฑ๊ณตํ•œ Promise๋งŒ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์ฒ˜๋ฆฌ
const recentProductInfo = recentProductInfoResults.flatMap((result) => {
if (result.status === 'fulfilled' && result.value) {
return result.value;
} else {
return [];
}
});
const productList = recentProductInfo.map((data) => `product:${data.productId}`);
const cacheData = await this.redis.mget(productList); // redis ์ ‘๊ทผ ํšŸ์ˆ˜ ์ค„์ด๊ธฐ ์œ„ํ•œ ์ž„์‹œ ๋ฐฉํŽธ
const checkProducts = await Promise.all(
Expand Down

0 comments on commit 4901615

Please sign in to comment.