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

230
Visualizações
How to avoid ` PromiseRejectionHandledWarning: Promise rejection was handled asynchronously`?

I'm getting PromiseRejectionHandledWarning: Promise rejection was handled asynchronously warning for my code, it also fails Jest tests. I've read that's Promise rejection should be handled at the same place they're defined and seem to understand the logic and the reasons. But it looks like something is wrong with my understanding as what I expect to be synchronous handling still cause warning. My typescript code is below. Promise, that is rejected is sendNotification(subscription, JSON.stringify(message)). From my understanding it's handled right away using .catch call, but probably I'm missing something. Can anyone, please, point me to my mistake?

private notify(tokens: string[], message: IIterableObject): Promise<any> {
    const promises = [];
    tokens.forEach(token => {
        const subscription = JSON.parse(token);
        this.logger.log('Sending notification to subscription', {subscription, message})
        const result = this.WebPushClient
            .sendNotification(subscription, JSON.stringify(message))
            .catch(e => {
                this.logger.log('Send notification failed, revoking token', {
                    subscription,
                    message,
                    token,
                    e
                })
                return this.revokeToken(token).catch(error => {
                    this.logger.error('Failed to revoke token', {
                        token,
                        error,
                    })
                    return Promise.resolve();
                });
            });
        promises.push(result);
    });

    return Promise.all(promises);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found the issue. In Jest you can't just mock return value with rejected promise. You need to wrap it in special workaround function:

const safeReject = p => {
    p.catch(ignore=>ignore);
    return p;
};

And then wrap the Promise before return it

const sendNotification = jest.fn();
sendNotification.mockReturnValue(safeReject(Promise.reject(Error('test error'))));
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