all()
all<
T>(promises):Promise<{ -readonly [K in string | number | symbol]: Awaited<T[K<K>]> }>
all<
T>(promises):Promise<{ [K in string | number | symbol]: Awaited<T[K]> }>
Executes multiple promises in parallel and returns their results.
Extends Promise.all() with object support for structured results.
Object input returns named results; array input behaves like Promise.all().
Type Parametersโ
T: T extends readonly unknown[]โ
The type of the promise results.
Parametersโ
promises: Tโ
Array or object of promises to execute.
Returns: Promise<{ -readonly [K in string | number | symbol]: Awaited<T[K<K>]> }>โ
Promise that resolves to the results (array or object matching input shape).
Throwsโ
Rejects if any input promise rejects.
Sinceโ
1.1.0
Also known asโ
all (Radashi, Effect) ยท โ (Lodash, es-toolkit, Remeda, Ramda, Modern Dash, Antfu)
Exampleโ
// Array of promises
const [user, posts] = await all([fetchUser(), fetchPosts()]);
// Object of promises (named results)
const { user, posts } = await all({
user: fetchUser(),
posts: fetchPosts()
});
How it works?โ
Executes multiple promises concurrently and collects their results. Supports both array and object inputs โ object input returns named results.
Object Input (Named Results)โ
Use Casesโ
Load multiple resources simultaneously ๐โ
Execute multiple API calls in parallel to improve performance and reduce loading times. Essential for dashboard applications and data-heavy interfaces.
// โ
With all() - Structured and intuitive
const dashboardData = await all({
user: fetchUser(userId),
posts: fetchUserPosts(userId),
notifications: fetchNotifications(userId),
settings: fetchUserSettings(userId),
});
console.log(dashboardData);
// {
// user: { id: 1, name: "John", email: "john@example.com" },
// posts: [{ id: 1, title: "My First Post" }],
// notifications: [{ id: 1, message: "Welcome!" }],
// settings: { theme: "dark", notifications: true }
// }
// โ With Promise.all() - Positional and less intuitive
const [user, posts, notifications, settings] = await Promise.all([
fetchUser(userId),
fetchUserPosts(userId),
fetchNotifications(userId),
fetchUserSettings(userId),
]);
// Hard to remember which position corresponds to which data
Combine related data arraysโ
Merge related arrays into paired tuples for coordinated processing and analysis. Essential for data correlation and maintaining relationships between datasets.
// Load related data arrays
const [users, posts, comments] = await all([
fetchUsers(),
fetchPosts(),
fetchComments(),
]);
// Process correlated data
const userPostCounts = users.map((user) => ({
...user,
postCount: posts.filter((post) => post.userId === user.id).length,
}));
Initialize application state ๐โ
Load all required data for application initialization in parallel. Critical for reducing application startup time and improving user experience.
// Initialize app with all required data
const appState = await all({
config: loadAppConfig(),
user: getCurrentUser(),
permissions: getUserPermissions(),
theme: loadTheme(),
language: loadLanguage(),
});
// App is ready with all data loaded
initializeApp(appState);
Validate multiple inputs simultaneouslyโ
Check multiple validation rules in parallel for faster form processing. Essential for complex forms with multiple validation requirements.
// Validate form inputs in parallel
const validationResults = await all({
email: validateEmail(formData.email),
password: validatePassword(formData.password),
username: validateUsername(formData.username),
terms: validateTermsAcceptance(formData.terms),
});
if (Object.values(validationResults).every((result) => result.isValid)) {
// All validations passed
submitForm(formData);
}
Process batch operationsโ
Execute multiple independent operations in parallel for better performance. Critical for batch processing systems and data pipelines.
// Process multiple files in parallel
const processedFiles = await all([
processImageFile("image1.jpg"),
processImageFile("image2.jpg"),
processImageFile("image3.jpg"),
processImageFile("image4.jpg"),
]);
console.log(`Processed ${processedFiles.length} files successfully`);