Sequential Promise Execution
MediumWrite sequentialPromises(items, asyncFn) function that executes an async function for each array element sequentially (one after another).
Requirements:
- Takes an array of items and an async function
- Calls asyncFn for each item in order
- Next call starts only after previous one completes
- Returns an array of all results in execution order
- If any call fails - stops and throws error
- Empty array returns []
Example:
const items = ['item1', 'item2', 'item3'];
const postData = async (item) => {
await delay(100);
return `posted: ${item}`;
};
const results = await sequentialPromises(items, postData);
// results = ['posted: item1', 'posted: item2', 'posted: item3']
// Total ~300ms (100ms * 3)JavaScript•UTF-8
Run your code to see results.
Click the Run button above
Mobile view - please use desktop for better experience