From c6f7365295c009487609bc25df004aaee73a7cb1 Mon Sep 17 00:00:00 2001 From: MichaelAblerCode <161170520+MichaelAblerCode@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:41:32 +0100 Subject: [PATCH] Update asynchronous-flow-control.md -Fix: Corrected description to reflect serial execution order instead of limited parallel. Rearranged order to logically sequence "limited in series" after "in series" for better clarity. Signed-off-by: MichaelAblerCode <161170520+MichaelAblerCode@users.noreply.github.com> --- .../asynchronous-flow-control.md | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md b/apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md index 8b83359e19271..3fdaeebe8a2d7 100644 --- a/apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md +++ b/apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md @@ -173,7 +173,48 @@ function serialProcedure(operation) { serialProcedure(operations.shift()); ``` -2. **Full parallel:** when ordering is not an issue, such as emailing a list of 1,000,000 email recipients. +2. **Limited in series:** functions will be executed in a strict sequential order, but with a limit on the number of executions. Useful when you need to process a large list but with a cap on the number of items successfully processed. + +```js +let successCount = 0; + +function final() { + console.log(`dispatched ${successCount} emails`); + console.log('finished'); +} + +function dispatch(recipient, callback) { + // `sendEmail` is a hypothetical SMTP client + sendMail( + { + subject: 'Dinner tonight', + message: 'We have lots of cabbage on the plate. You coming?', + smtp: recipient.email, + }, + callback + ); +} + +function sendOneMillionEmailsOnly() { + getListOfTenMillionGreatEmails(function (err, bigList) { + if (err) throw err; + + function serial(recipient) { + if (!recipient || successCount >= 1000000) return final(); + dispatch(recipient, function (_err) { + if (!_err) successCount += 1; + serial(bigList.pop()); + }); + } + + serial(bigList.pop()); + }); +} + +sendOneMillionEmailsOnly(); +``` + +3. **Full parallel:** when ordering is not an issue, such as emailing a list of 1,000,000 email recipients. ```js let count = 0; @@ -227,45 +268,4 @@ recipients.forEach(function (recipient) { }); ``` -3. **Limited parallel:** parallel with limit, such as successfully emailing 1,000,000 recipients from a list of 10 million users. - -```js -let successCount = 0; - -function final() { - console.log(`dispatched ${successCount} emails`); - console.log('finished'); -} - -function dispatch(recipient, callback) { - // `sendEmail` is a hypothetical SMTP client - sendMail( - { - subject: 'Dinner tonight', - message: 'We have lots of cabbage on the plate. You coming?', - smtp: recipient.email, - }, - callback - ); -} - -function sendOneMillionEmailsOnly() { - getListOfTenMillionGreatEmails(function (err, bigList) { - if (err) throw err; - - function serial(recipient) { - if (!recipient || successCount >= 1000000) return final(); - dispatch(recipient, function (_err) { - if (!_err) successCount += 1; - serial(bigList.pop()); - }); - } - - serial(bigList.pop()); - }); -} - -sendOneMillionEmailsOnly(); -``` - Each has its own use cases, benefits, and issues you can experiment and read about in more detail. Most importantly, remember to modularize your operations and use callbacks! If you feel any doubt, treat everything as if it were middleware!