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

117
Vistas
Is a promise resolved when I add .catch to it?

I am new to typescript / javascript, so I don't know much about promises. Here is my use-case: I am creating three different promises inside my cloud-function and then returning it with Promise.all([promise1, promise2, promise3]). Each of these promises are created inside a function with "return Promise...".

My question is, when I add ".catch" inside these functions, will Promise.all still work?. Does it make any difference returning someServiceThatCreatesPromise() with and without .catch()?

Code

export async function myCloudFirestoreFunction() {
   try  {
        const myFirstPromise = createFirstPromise()
        const mySecondPromise = createSecondPromise()
        const thirdPromise = createThirdPromise()

        return Promise.all([
          myFirstPromise,
          mySecondPromise,
          myThirdPromise
       ]);
   } catch (err) {
       functions.logger.err(`Something bad happened, See: ${(err as Error).message}`
   }
}

// Difference with and without `.catch`?
function createFirstPromise() {
   return someServiceThatCreatesPromise().catch((err) => { // LOGGING });
}

// Difference with and without `.catch`?
function createSecondPromise() {
   return someServiceThatCreatesPromise().catch((err) => { // LOGGING });
}

// Difference with and without `.catch`?
function createThirdPromise() {
   return someServiceThatCreatesPromise().catch((err) => { // LOGGING });
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Adding .catch inside createNPromise won't affect anything assuming all your Promises resolve and do not reject.

However, if one of the Promises rejects and you catch it within the .catch method, then it won't work as you're expecting unless you re-throw that error and catch it again inside the try/catch in your myCloudFirestoreFunction function.

async function myCloudFirestoreFunction() {
  try {
    const result = await Promise.all([
      createFirstPromise(),
      createSecondPromise()
    ]);
  } catch (error) {
    console.log({ error });
  }
}

function createFirstPromise() {
  return Promise.reject("Oof").catch((e) => {
    // do work, e.g. log, then
    // pass the error forward so that it can be caught
    // inside the caller
    throw e;
  });
}

function createSecondPromise() {
  return Promise.resolve("value");
}

myCloudFirestoreFunction();

Alternatively, you just catch errors inside the caller (myCloudFirestoreFunction) instead of catching them separately.

async function myCloudFirestoreFunction() {
  const result = await Promise.all([
    createFirstPromise(),
    createSecondPromise()
  ]).catch((err) => console.log({ err }));
}

function createFirstPromise() {
  return Promise.reject("Oof");
}

function createSecondPromise() {
  return Promise.resolve("value");
}

myCloudFirestoreFunction();

about 4 years ago · Juan Pablo Isaza Denunciar

0

when I add ".catch" inside these functions, will Promise.all still work?

Calling catch() on a promise does not in any way change the way the original promise works. It is just attaching a callback that gets invoked when the first promise becomes rejected, and also returning another promise that resolves after the original promise is fulfilled or rejected.

Does it make any difference returning someServiceThatCreatesPromise() with and without .catch()?

It would not make any difference to the code that depends on the returned promise. Both the original promise and the one returned by catch() will tell downstream code when the original work is done by becoming fulfilled.

I suggest reading comprehensive documentation on promises, for example:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

There is a diagram in there that you can follow to understand what happens at each turn. Also in that document, you will read:

Processing continues to the next link of the chain even when a .then() lacks a callback function that returns a Promise object. Therefore, a chain can safely omit every rejection callback function until the final .catch().

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