Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!