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

231
Vistas
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 Respuestas
Responde la pregunta

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 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