Probé esta solución para obtener datos de varias API con espera asíncrona, pero la salida no está definida, ¿qué podría estar saliendo mal?
const getData = async (jwt) => { try { const [response1, response2, response3] = await Promise.all( [ fetch("http://localhost:3000/api/confirmed"), fetch("http://localhost:3000/api/deaths"), fetch("http://localhost:3000/api/recovered"), ], { method: "GET", headers: { "user-agent": "vscode-restclient", "content-type": "application/json", accept: "application/json", authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6IkxlYW5uZSBHcmFoYW0iLCJ1c2VybmFtZSI6IkJyZXQiLCJpYXQiOjE1OTY1MDY4OTN9.KDSlP9ALDLvyy0Jfz52x8NePUejWOV_mZS6cq4-JZXs", }, } ); const { data1 } = await response1.json(); const { data2 } = await response2.json(); const { data3 } = await response3.json(); console.log(data1, data2, data3); } catch (err) { console.error(`Error: ${err}`); } }; getData();Promise.all solo acepta un único parámetro, una serie de promesas. Por lo tanto, su segundo parámetro (el objeto con method y headers ) simplemente se ignora. Ese objeto debe pasarse como segundo parámetro a la función de fetch , no a Promise.all .
Prueba lo siguiente:
const getData = async (jwt) => { try { const fetchOptions = { method: "GET", headers: { "user-agent": "vscode-restclient", "content-type": "application/json", accept: "application/json", authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6IkxlYW5uZSBHcmFoYW0iLCJ1c2VybmFtZSI6IkJyZXQiLCJpYXQiOjE1OTY1MDY4OTN9.KDSlP9ALDLvyy0Jfz52x8NePUejWOV_mZS6cq4-JZXs", }, }; const [response1, response2, response3] = await Promise.all( [ fetch("http://localhost:3000/api/confirmed", fetchOptions), fetch("http://localhost:3000/api/deaths", fetchOptions), fetch("http://localhost:3000/api/recovered", fetchOptions), ], ); const { data1 } = await response1.json(); const { data2 } = await response2.json(); const { data3 } = await response3.json(); console.log(data1, data2, data3); } catch (err) { console.error(`Error: ${err}`); } }; getData();