Wait for all promises to settle, whether they resolve or reject.
Code
Generalconst promises = [
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(new Error('fail'))
];
const results = await Promise.allSettled(promises);
const fulfilled = results
.filter(r => r.status === 'fulfilled')
.map(r => r.value);
const rejected = results
.filter(r => r.status === 'rejected')
.map(r => r.reason);
return `fulfilled: ${fulfilled.length}, rejected: ${rejected.length}`;Browser·fetch() may be limited by CORS
More JavaScript Snippets
Debounce
Create a debounced function that delays execution until after a specified wait period has elapsed since the last call.
Memoize
Cache function results to avoid redundant calculations and improve performance for expensive operations.
Once
Create a function that can only be called once, returning the cached result for subsequent invocations.
Parallel with Limit
Run promises in parallel with a concurrency limit to control resource usage and prevent overwhelming external services.
Promise.any
Return the result of the first promise that resolves successfully.
Retry with Backoff
Retry an async function with exponential backoff to handle transient failures gracefully.