ð Promise.all ã§éåæåŠçã䞊è¡å®è¡ãããïŒ
æçš¿æ¥: 2025幎06æ22æ¥
JavaScriptã§ã¯ãAPIã®åŒã³åºããå€éšãµãŒãã¹ãšã®éä¿¡ãªã©ãéåæåŠçããæ±ãå Žé¢ããããããŸãã
ããšãã°ããA瀟ã®APIã«éç¥ãéãããB瀟ã«ã¡ãŒã«ãéãããšãã£ã2ã€ã®åŠçããããšãããããããé çªã«å®è¡ããŠãããšæéããããããšããããŸãã
ããã§äŸ¿å©ãªã®ã Promise.all
ã§ãã
ãŸãã¯éåæåŠçãé çªã«å®è¡ããŠããäŸã§ãã
// A瀟ã«éç¥ãéã
await notifyServiceA("ã¡ãã»ãŒãžA");
// B瀟ã«ã¡ãŒã«ãéã
const result = await sendEmail({
to: "example@example.com",
subject: "ããã«ã¡ã¯",
body: "æ¬æã§ã",
});
ãã®ã³ãŒãã¯ãA瀟ãžã®éç¥ãå®äºããŠããB瀟ãžã®ã¡ãŒã«éä¿¡ãå§ãŸãã®ã§ãæéãããããŸãã
Promise.all
ã䜿ã£ãŠäžŠè¡åŠçPromise.all
ã䜿ãã°ã2ã€ã®åŠçãåæã«å®è¡ã§ããŸãã
try {
const [notifyResult, emailResult] = await Promise.all([
notifyServiceA("ã¡ãã»ãŒãžA"),
sendEmail({ to: "...", subject: "...", body: "..." }),
]);
} catch (error) {
console.error("ã©ã¡ããã®åŠçã§ãšã©ãŒãçºçããŸãã", error);
}
Slackéç¥ãšã¡ãŒã«éä¿¡
ããŒã¿ããŒã¹ä¿åãšå€éšAPIãžã®é£æº
è€æ°ã®ãã¡ã€ã«ã¢ããããŒã
ãªã©ããè€æ°ã®ç¬ç«ããåŠçãããããšãã« Promise.all
ã¯éåžžã«åœ¹ç«ã¡ãŸãã
Promise.all
ã䜿ãã°ãããªãã®ã¢ããªã®åŸ
ã¡æéãççž®ãããŠãŒã¶ãŒäœéšãåäžãããããšãã§ããŸãããã²æŽ»çšããŠã¿ãŠãã ããïŒ
Promise.all() - MDN Web Docs
ãð Promise.all
ã®åºæ¬çãªäœ¿ãæ¹ããè¿ããã Promise ã®æåã«ã€ããŠè©³ãã解説ãããŠããŸããäŸãè±å¯ã§éåžžã«ããããããã§ãã
Promise.all - JavaScript.info
ãð å®éã®ãŠãŒã¹ã±ãŒã¹ã泚æç¹ã倱ææã®åäœãªã©ãäžå¯§ã«èª¬æãããŠããŸãã
13ç« å®å課é¡ãçµããŠã®ææ³
ãããŒã éçºãæè¡çã«å€§å€ã ã£ãããš
ã13ç« ãå®åäœéšèª²é¡ã®ææ³
ããŒã éçºã®æ¯ãè¿ã
ãŸãããã粟ç¥ã®æ³šæç¹
ã¿ããªçé¢ç®éããïŒãŸãããã粟ç¥ã®ããã
TypeScriptã®åå®å šã殺ã!? å人çã¢ã³ããã¿ãŒã³ð¥
åå¿è åãïŒ3ã€ã®ãã¿ãŒã³ã®ã³ãŒãæ¯èŒã§çè§£ããNext.jsã®ã³ã³ããŒãã³ãåå²
NestJSã®ãã³ã¬ãŒã¿ãŒãšã¯ïŒ@Controllerã@Injectableã®æå³ãšäœ¿ãæ¹ãåå¿è åã
NestJSåå¿è åãïŒAppModule / Controller / Serviceã®åºæ¬æ§æãšåäœã®æµã
Nest.jsã®åºæ¬æ§æã¬ã€ã
ã³ãã¥ããã£ã楜ããããã®ã¹ããã