Use for...of loop with await to run promises in sequence
const urls = [url1, url2, url3];
const results = [];
for (const url of urls) {
const data = await fetch(url);
results.push(data);
}async function processSequentially(items) {
const results = [];
for (const item of items) {
console.log(`Processing ${item}...`);
await sleep(1000); // Wait 1 second
results.push(`Processed: ${item}`);
}
return results;
}
// Using reduce for sequential promises
async function sequence(tasks) {
return tasks.reduce(async (promise, task) => {
const results = await promise;
const result = await task();
return [...results, result];
}, Promise.resolve([]));
}
// Process in batches
async function batchProcess(items, batchSize) {
const results = [];
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
const batchResults = await Promise.all(
batch.map(item => processItem(item))
);
results.push(...batchResults);
}
return results;
}for (const task of tasks) {
try {
await task();
} catch (error) {
console.error('Task failed:', error);
// Continue with next task
}
}const result = await items.reduce(
async (acc, item) => [...await acc, await process(item)],
Promise.resolve([])
);Avoid Promise.all() when order matters
// DON'T DO THIS if sequential order is required
await Promise.all(urls.map(url => fetch(url)));✓ Works in all modern browsers (ES2017+)