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

121
Vistas
Why not in caller's catch() but the try's catch?

I have an interesting case that a Promise.reject() not caught in its caller's .catch() but in the try{ }catch{ }.

callDB() calls a pg db function to perform an Insert, when a run-time error (unique-key violation) raised, it goes to .then().catch():

const callDB = (param) =>
{
    return new Promise((resolve, reject) =>
    {
        var sql = "...myInsert($1, $2)";
        var dataValue = [param.p1, paramp2];
        db.any(sql, dataValue)
            .then
            (
                good => resolve(good),
                bad => reject(bad)
            )
            // Run-Time Err like unique-key violation caught here, returns a reject.
            .catch 
            ( dbEr => { return reject(dbEr); })
    });
}

f2(), the caller, its callDB().catch() not catching the reject, but the try{} catch{tce}:

async function f2()
{
    try
    {
        ... 
        // callDB's reject NOT caught here.
        const good2  = await callDB(param).catch((e2)=>{ return Promise.reject(e2); });  
        ...
    }
    catch(tce)
    {
        // callDB's reject caught here!
        return Promise.reject(tce); 
    }
}

An Azure Function calls f2():

module.exports = async function f1(context)   
{
    try
    {
        await f2().then
        (
            good => ...
            bad => 
            {
                // Caught here as Expected!
                ...
                context.res = {status: 401, body: bad};     
            }
        )
        .catch
        (
            (e1) => 
            {
                context.res = {status: 401, body: "never reach e1"};
            }
        )
    }
    catch (etry)
    {
        context.res = {status: 401, body: "never reach etry"};
    }
}    

My questions are:

  1. why the callDB's reject goes into f2's try{} catch{}?
  2. what's the difference between .then().catch() and try{} catch{}?
about 4 years ago · Juan Pablo Isaza
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