Skip to content

Promise rejection with finally, does not throw unhandled rejection #47562

Closed as duplicate

Description

@sviat9440

Which @angular/* package(s) are the source of the bug?

zone.js

Is this a regression?

No

Description

Promise rejection with finally, does not throw unhandled rejection

Please provide a link to a minimal reproduction of the bug

https://github.com/sviat9440/zone-js-bug-reproduction.git

Please provide the exception or error you saw

Expected

  1. If the promise is rejected, the final callback should be executed
  2. After that, it should throw the UnhandledPromiseRejection error

Actual without zone.js

Promise.reject(new Error('test error')).finally(() => console.log('finally'));

Output:

finally
file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/work-as-expected.js:1
Promise.reject(new Error('test error')).finally(() => console.log('finally'));
               ^

Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/work-as-expected.js:1:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

Browser output:
image

Work as expected

Actual with zone.js

Case 1

import 'zone.js';

Promise.reject(new Error('test error')).finally(() => console.log('finally'));

Output:

finally

Process finished with exit code 0

Browser output:
image

Case 2

import 'zone.js';

Promise.reject(new Error('test error'));

Output:

Unhandled Promise rejection: test error ; Zone: <root> ; Task: null ; Value: Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/not-work-as-expected.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/not-work-as-expected.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 0

Browser output:
image

UnhandledPromiseRejection error is thrown only without finally callback, exit code was 0

Workaround

import 'zone.js';

try {
  await Promise.reject(new Error('test error'));
} finally {
  console.log('finally');
}

Output:

Unhandled Promise rejection: test error ; Zone: <root> ; Task: null ; Value: Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
finally
node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

Browser output:
image

Error reported twice: UnhandledPromiseRejection and Error('test error'). Expected no UnhandledPromiseRejection from Zone

Side effect

Unhandled errors in promises with finally callback is missing. Console is empty. Logging systems also do not see such errors.

In browser the same situation.

Please provide the environment you discovered this bug in (run ng version)

"zone.js": "^0.11.8"

Anything else?

No response

Activity

  1. changed the title [-]Promise rejection with finnaly, does not throw unhandled rejection[/-] [+]Promise rejection with finally, does not throw unhandled rejection[/+] on Sep 27, 2022
  2. AndrewKushnir commented on Sep 28, 2022

    @AndrewKushnir
    Contributor

    Thanks for reporting the issue. It looks related to (should be fixed by?) the change in PR #46462.

  3. added
    area: zonesIssues related to zone.js
    P4A relatively minor issue that is not relevant to core functions
    on Sep 28, 2022
  4. modified the milestone: Backlog on Sep 28, 2022
  5. sviat9440 commented on Sep 28, 2022

    @sviat9440
    Author

    The last activity there was on June 15. Are there any progress?

  6. JiaLiPassion commented on Mar 26, 2023

    @JiaLiPassion
    Contributor

    I created a PR to fix this one. #49589

  7. self-assigned this
    on Mar 31, 2023
  8. angular-automatic-lock-bot commented on Oct 3, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functionsarea: zonesIssues related to zone.jsstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions