Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(git): merge main into dev #792

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions content/til/2024_02_23-promiseall.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: 'Promise.all'
description: 'Otimizando chamadas assíncronas no Javascript'
date: '2024-02-23'
tags: [javascript,async]
---

**Dica JavaScript**: Otimize Chamadas Assíncronas com `Promise.all`

Executar `await` em chamadas assíncronas de forma sequencial, como em `getProducts()` seguido por `getOrders()`, faz com que a segunda chamada espere a conclusão da primeira, aumentando o tempo total de execução.

Optar por `Promise.all` nesse caso, permite que as chamadas sejam feitas em paralelo, reduzindo o tempo de espera e melhorando a performance da sua aplicação.

❌ **worse**
```javascript
async function getData() {
const products = await getProducts()
const orders = await getOrders()
}
```

✅ **better**
```javascript
async function getData() {
const [products, orders] = await Promise.all([
getProducts(), getOrders()
])
}
```

Mas o `Promise.all` deve ser usado com cuidado, porque se uma das requisições falhar, todas vão falhar.

*ref*: https://www.linkedin.com/feed/update/urn:li:activity:7166552833750773760/
Loading