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

187
Visualizações
Handle errors inside promise scope funcions

I have an async function which return promise In this promise I have scope function and inside that I have 2 other scope functions If I try to throw error on function a the code continues it`s execution

    async someAsyncFunc(): Promise<void> {
      return new Promise((resolve, reject) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let port: any;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let writer: any;
        const test = async () => {
          const a = async () => {
            throw new Error();
          };

          const b = async () => {
            // do some work
            await a();
          };

          try {
            await b();
          } catch (err) {
            console.log(err);
          }

          resolve();
        };

        try {
          test().catch((err) => {
            console.log("catch block error");
            console.error(err);
            reject(err);
          });
        } catch (error) {
          return reject(error);
        } finally {
        }
      });
    }

I want somehow to handle all errors that could happen on either functions a,b and load at this final try catch block of the returned promise

try {
    test().catch((err) => {
        console.log("catch block error");
        console.error(err);
        reject(err);
          });
        } catch (error) {
          return reject(error);
        } finally {
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

      try {
        await b();
      } catch (err) {
        console.log(err);
      }

You're catching and handling the error there.

You can't get it in test()'s catch handler because no error is raised to that point.

It's already been handled.

You'd need to throw it again if you want something else to catch it:

  try {
    await b();
  } catch (err) {
    console.log(err);
    throw err;
  }
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