-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.js
More file actions
87 lines (69 loc) · 2.8 KB
/
10.js
File metadata and controls
87 lines (69 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Ejecución secuencial innecesaria
const usuario = await obtenerUsuario(1); // 300ms
const productos = await obtenerProductos(); // 200ms
const config = await obtenerConfig(); // 400ms
Promise.all
const fetchProducto = id =>
fetch(`https://api.escuelajs.co/api/v1/products/${id}`)
.then(r => {
if (!r.ok) throw new Error(`Error ${r.status}`);
return r.json();
});
// Si todas tienen éxito, funciona
Promise.all([fetchProducto(4), fetchProducto(5), fetchProducto(6)])
.then(productos => {
productos.forEach(p => console.log(`${p.title} - $${p.price}`));
})
.catch(error => console.error(error.message));
//Si una falla, todas fallan
Promise.all([fetchProducto(4), fetchProducto(999), fetchProducto(6)])
.then(productos => {
productos.forEach(p => console.log(`${p.title} - $${p.price}`));
})
.catch(error => console.error(error.message));
// Promise.allSettled: espera a que todas las promesas terminen sin importar el resultado
Promise.allSettled([
Promise.resolve("éxito"),
Promise.reject(new Error("fallo")),
Promise.resolve("también éxito"),
])
.then(resultados => {
resultados.forEach(resultado => {
if (resultado.status === "fulfilled") {
console.log("✅ Valor:", resultado.value);
} else {
console.log("❌ Error:", resultado.reason.message);
}
});
});
// Promise.race: devuelve la primera promesa que se resuelva
const rapida = new Promise(resolve => setTimeout(() => resolve("rápida"), 100));
const lenta = new Promise(resolve => setTimeout(() => resolve("lenta"), 500));
const mediana = new Promise(resolve => setTimeout(() => resolve("mediana"), 300));
Promise.race([rapida, lenta, mediana])
.then(ganadora => console.log("Ganó:", ganadora));
// Ejemplo práctico de Promise.race: timeout en peticiones
const peticion = fetch("https://api.escuelajs.co/api/v1/products/4");
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error("Tiempo de espera agotado")), 1000)
);
Promise.race([peticion, timeout])
.then(res => res.json())
.then(producto => console.log(`${producto.title} - $${producto.price}`))
.catch(error => console.error(error.message)); // "Tiempo de espera agotado"
// Promise.any: devuelve la primera promesa que se resuelva exitosamente
Promise.any([
Promise.reject(new Error("fallo 1")),
Promise.resolve("éxito"), // ← Esta gana
Promise.resolve("también éxito"),
])
.then(primero => console.log("Primera exitosa:", primero)); // "éxito"
// Si todas fallan, lanza un AggregateError
Promise.any([
Promise.reject(new Error("fallo 1")),
Promise.reject(new Error("fallo 2")),
])
.catch(error => {
console.error(error.constructor.name); // "AggregateError"
console.error(error.errors); // [Error: fallo 1, Error: fallo 2]
});