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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners