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

139
Visualizações
How can I catch an unhandled promise rejection locally without 'await' or 'Promise.catch'
const asyncThrowSampleApiFunction = async () => {
  throw new Error('SOMETHING');
};

const MyComponent = (SomeApiFunction) => {
  try {
    SomeApiFunction();
  } catch (err) {
    handleMyError(err);
  }

  return ...;
};

const c = MyComponent(() => {
  asyncThrowSampleApiFunction();    // call async function without 'await' or 'Promise.catch'
});

Is there any way to catch an unhandled promise rejection without Promise.catch or await?

In React.useEffect, we often use async function call without await statement. And I want to handle possible asynchronous errors in React.useEffect.

So I want to catch an unhandled promise rejection, but I can only find a global way to catch these rejection types.

(e.g. window.addEventListener('unhandledrejection', ...); )

I want to make my own ErrorBoundary. So I need to catch these rejections locally.

Is there any way to catch them?

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

How can I catch an unhandled promise rejection locally without 'await' or 'Promise.catch'

You must catch a rejected promise somewhere with either .catch(), with the second argument to .then() or by bracketing an await with a try/catch. Those are your only three options.

So, technically if you're looking for something different than await or .catch(), you could use the second argument to .then() - though I suspect that's not really what you were asking for in which case, there are no other options.

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