Skip to content

Commit 69d004a

Browse files
docs: correctly reflect serial execution order instead of limited parallel. (#7335)
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 <[email protected]>
1 parent ebc0e82 commit 69d004a

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md

+42-42
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,48 @@ function serialProcedure(operation) {
173173
serialProcedure(operations.shift());
174174
```
175175

176-
2. **Full parallel:** when ordering is not an issue, such as emailing a list of 1,000,000 email recipients.
176+
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.
177+
178+
```js
179+
let successCount = 0;
180+
181+
function final() {
182+
console.log(`dispatched ${successCount} emails`);
183+
console.log('finished');
184+
}
185+
186+
function dispatch(recipient, callback) {
187+
// `sendEmail` is a hypothetical SMTP client
188+
sendMail(
189+
{
190+
subject: 'Dinner tonight',
191+
message: 'We have lots of cabbage on the plate. You coming?',
192+
smtp: recipient.email,
193+
},
194+
callback
195+
);
196+
}
197+
198+
function sendOneMillionEmailsOnly() {
199+
getListOfTenMillionGreatEmails(function (err, bigList) {
200+
if (err) throw err;
201+
202+
function serial(recipient) {
203+
if (!recipient || successCount >= 1000000) return final();
204+
dispatch(recipient, function (_err) {
205+
if (!_err) successCount += 1;
206+
serial(bigList.pop());
207+
});
208+
}
209+
210+
serial(bigList.pop());
211+
});
212+
}
213+
214+
sendOneMillionEmailsOnly();
215+
```
216+
217+
3. **Full parallel:** when ordering is not an issue, such as emailing a list of 1,000,000 email recipients.
177218

178219
```js
179220
let count = 0;
@@ -227,45 +268,4 @@ recipients.forEach(function (recipient) {
227268
});
228269
```
229270

230-
3. **Limited parallel:** parallel with limit, such as successfully emailing 1,000,000 recipients from a list of 10 million users.
231-
232-
```js
233-
let successCount = 0;
234-
235-
function final() {
236-
console.log(`dispatched ${successCount} emails`);
237-
console.log('finished');
238-
}
239-
240-
function dispatch(recipient, callback) {
241-
// `sendEmail` is a hypothetical SMTP client
242-
sendMail(
243-
{
244-
subject: 'Dinner tonight',
245-
message: 'We have lots of cabbage on the plate. You coming?',
246-
smtp: recipient.email,
247-
},
248-
callback
249-
);
250-
}
251-
252-
function sendOneMillionEmailsOnly() {
253-
getListOfTenMillionGreatEmails(function (err, bigList) {
254-
if (err) throw err;
255-
256-
function serial(recipient) {
257-
if (!recipient || successCount >= 1000000) return final();
258-
dispatch(recipient, function (_err) {
259-
if (!_err) successCount += 1;
260-
serial(bigList.pop());
261-
});
262-
}
263-
264-
serial(bigList.pop());
265-
});
266-
}
267-
268-
sendOneMillionEmailsOnly();
269-
```
270-
271271
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!

0 commit comments

Comments
 (0)