Skip to content

What should the behavior of an exception inside a promise be? #5787

@WieserSoftware

Description

@WieserSoftware

Is this a bug report?

No/Maybe

I have a section of code (in typescript using "react-scripts": "2.1.1") that generates a promise that is used later. Inside that promise, I occasionally will get a bad object, and expect the promise to be rejected as a result of an object being undefined. Here's a pathological example, where I force the variable a to be undefined to illustrate the problem, which gives
Unhandled Rejection (TypeError): Cannot read property 'toLowerCase' of undefined

    const a: any = undefined;
    cached = new Promise(async (resolve, reject) => {
         const ret = a.toLowerCase();
         resolve(ret);
   });

On chrome this generates an exception at runtime when running the debug build, which prevents any further use of the app, but does not on edge.

If instead, I rewrite it like this, it also fails on chrome.

    const a: any = undefined;
    cached = new Promise(async (resolve, reject) => {
       try {
         const ret = a.toLowerCase();
         resolve(ret);
       } catch (e) {
            throw(e);
       }
   });

This too fails, making it look like anything thrown that derives from error causes the message:

    const a: any = undefined;
    cached = new Promise(async (resolve, reject) => {
       try {
         const ret = a.toLowerCase();
         resolve(ret);
       } catch (e) {
            throw new Error("fix chrome");
       }
   });

however if I rewrite it like this, it does not fail on chrome.

    const a: any = undefined;
    cached = new Promise(async (resolve, reject) => {
       try {
         const ret = a.toLowerCase();
         resolve(ret);
       } catch (e) {
            throw "fix chrome";
       }
   });

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions