Source: chrysanthos.xyz

Don’t ever use Promise.all()
Imagine that you have two asynchronous calls like so: async function getData() { const users = await fetchUsers(); const categories = await fetchCategories(); } These two API calls are run sequentially, meaning that for “fetchCategories” to run, we will wait until “fetchUsers” is completed.

async function getData() { const [users, categories] = await Promise.all( fetchUsers(), fetchCategories(), )}

The (obvious) next step you would think would be to add a try-catch around it, something like the following: async function getData() { try { const [users, categories] = await Promise.all( fetchUsers(), fetchCategories(), )} catch (err) { reportToLogger(err); }}

async function getData() { const [userResult, categoryResult] = await Promise.allSettled( fetchUsers(), fetchCategories(), ) if (userResult.status === 'rejected') { const error = userResult.reason // handle error } else { const users = userResult.value } if (categoryResult.status === 'rejected') { const error = categoryResult.reason // handle error }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners