Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
UnhandledPromiseRejectionWarning aunque envuelto en try/catch()

Recibo el siguiente mensaje de error cuando intento ejecutar storeDirectory() .

¿Por qué mi instrucción catch() no detecta el error que aparece dentro addFile() ?

Tengo entendido que los errores surgen en cualquier declaración de captura "externa", pero ese no parece ser el caso en mi código.

 (node:21204) UnhandledPromiseRejectionWarning: Error: HTTP error 500 at addFile (/Users/joe/dev/node/db/dbFunctions.js:28:11) at processTicksAndRejections (internal/process/task_queues.js:95:5) (Use `node --trace-warnings ...` to show where the warning was created) (node:21204) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
 //dbFunctions.js ... const addFile = async (name, type, href) => { const response = await fetch("http://localhost:3002/files", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name, type, href }), }); if (!response.ok) { throw new Error(`HTTP error ${response.status}`); } return response.json(); };
 //app.js const { addFile } = require("./db/dbFunctions"); ... const storeDirectory = async () => { try { //create array of files const response = await getDirectory(); const arr = response.directory.items; //loop through array and add each item to database arr.map((item) => { //add item to DB addFile( item.name, item.type, item.href ); }); } catch (error) { console.log(error); } };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try solo detectará errores asincrónicos si se await la Promesa. Cambio

 arr.map((item) => { //add item to DB addFile( item.name, item.type, item.href ); });

a

 await Promise.all(arr.map((item) => //add item to DB addFile( item.name, item.type, item.href ); ));

asegurándose de devolver la Promesa del .map .

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda