Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda