Title: zone.js promises returning Unhandled Promise Rejection error even with catch blocks · Issue #51328 · angular/angular · GitHub
Open Graph Title: zone.js promises returning Unhandled Promise Rejection error even with catch blocks · Issue #51328 · angular/angular
X Title: zone.js promises returning Unhandled Promise Rejection error even with catch blocks · Issue #51328 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? No Description I've hit the same issue as described in #31680 and both this and that ticket have same root cause detailed bellow. Since I cannot reopene...
Open Graph Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? No Description I've hit the same issue as described in #31680 and both this and that ticket have same root cause...
X Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? No Description I've hit the same issue as described in #31680 and both this and that ticket have same root c...
Opengraph URL: https://github.com/angular/angular/issues/51328
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"zone.js promises returning Unhandled Promise Rejection error even with catch blocks","articleBody":"### Which @angular/* package(s) are the source of the bug?\r\n\r\nzone.js\r\n\r\n### Is this a regression?\r\n\r\nNo\r\n\r\n### Description\r\n\r\nI've hit the same issue as described in #31680 and both this and that ticket have same root cause detailed bellow. Since I cannot reopened old issue I've created a new one. That older ticket was closed with a fix that was for a separate issue and the fix was not relevant or helped in anyway.\r\n\r\n### Repro\r\nFollowing simplified code will emit unhandled promise rejection even tho there is a catch handler. This happens only when zone.js is loaded. Without zone.js, all majors browsers (Chrome, Safari, Firefox) don't report anything in this case.\r\n\r\n```typescript\r\nonunhandledrejection = (e) => { console.log(\"Unhandled promise rejection\", e) }\r\n\r\n(async function() {\r\n return Promise.reject(1);\r\n})().catch(e => {});\r\n// OR\r\n(async function() {\r\n await Promise.reject(2);\r\n})().catch(e => {});\r\n```\r\nUsing native Promise.reject will not emit unhandled rejection.\r\n\r\n### Root cause:\r\nzone.js schedules the reporting of unhandled rejections on the microtask queue as soon as the promise is rejected. In the case of `Promise.reject` it schedules as part of the reject call.\r\nJS async function calls `.then` on the returned inner promise (Promise.reject) on the next microtask tick. This is defined in the Ecmascript spec.\r\nzone.js will end up running the code to report unhandled rejections before JS async function has a chance to call `then` and as such zone.js ends emiting unhandled rejection.\r\n\r\nAll major browsers check if unhandled rejections exists at the end of the microtask checkpoint (i.e. after draining the queue) and if needed will schedule on normal queue a task (or what you call a macro task) to process&report unhandled rejections. This allows sufficient time for the async infrastructure to hook to the returned promise. In Chrome and Safari it even allows sufficient time to add catch handler in a callback run by setTimeout of delay 0.\r\n\r\nChrome:\r\nhttps://github.com/chromium/chromium/blob/89e00b6b959f5efb4bd7eec468973badb3093a3b/third_party/blink/renderer/platform/scheduler/common/event_loop.cc#L108\r\nhttps://github.com/chromium/chromium/blob/89e00b6b959f5efb4bd7eec468973badb3093a3b/third_party/blink/renderer/bindings/core/v8/rejected_promises.cc#L248-L252\r\nFirefox:\r\nhttps://github.com/mozilla/gecko-dev/blob/77dd6aa3810610949a5ff925e24de2f8c11377fd/xpcom/base/CycleCollectedJSContext.cpp#L473-L480\r\nSafari:\r\nhttps://github.com/WebKit/WebKit/blob/75a3eb3a5fe3dce1867d880faf785137963b234a/Source/WebCore/dom/Microtasks.cpp#L107\r\nhttps://github.com/WebKit/WebKit/blob/75a3eb3a5fe3dce1867d880faf785137963b234a/Source/WebCore/dom/RejectedPromiseTracker.cpp#L142-L144\r\n\r\n\r\n### Please provide a link to a minimal reproduction of the bug\r\n\r\n_No response_\r\n\r\n### Please provide the exception or error you saw\r\n\r\n```true\r\nUnhandled Promise rejection: 1 ; Zone: \u003croot> ; Task: null ; Value: 1 undefined\r\nUnhandled Promise rejection: 2 ; Zone: \u003croot> ; Task: null ; Value: 2 undefined\r\nUnhandled promise rejection PromiseRejectionEvent {isTrusted: true, reason: 1, type: 'unhandledrejection' ...}\r\nUncaught (in promise) 1\r\nUnhandled promise rejection PromiseRejectionEvent {isTrusted: true, reason: 2, type: 'unhandledrejection' ...}\r\nUncaught (in promise) 2\r\n```\r\n\r\n\r\n### Please provide the environment you discovered this bug in (run `ng version`)\r\n\r\n_No response_\r\n\r\n### Anything else?\r\n\r\n_No response_","author":{"url":"https://github.com/ioancris","@type":"Person","name":"ioancris"},"datePublished":"2023-08-10T23:59:34Z","url":"https://github.com/angular/angular/issues/51328"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a79ebe1d-0ae7-b671-8c94-91179b3611cd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BBE2:33D24F:16CB3CB:1D3546E:6B013FC2 |
| html-safe-nonce | f21dd60c8cb072853b4814bd5fc298a3682d73e2f9c44975580ec76998b6e483 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQkUyOjMzRDI0RjoxNkNCM0NCOjFEMzU0NkU6NkIwMTNGQzIiLCJ2aXNpdG9yX2lkIjoiNzMxNDQyODEwNzI4ODI0NzcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7fa7fddee2123e44f685da2bd888f45da14f84959b534f66cfd7cb62c1fb4e92 |
| hovercard-subject-tag | issue:1846032933 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/51328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4155e95339102d5d2468f9585fcbdc25f8a99bf808ce986b2d81ee5a9adf57aa/angular/angular/issues/51328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4155e95339102d5d2468f9585fcbdc25f8a99bf808ce986b2d81ee5a9adf57aa/angular/angular/issues/51328 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? No Description I've hit the same issue as described in #31680 and both this and that ticket have same root cause... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ioancris |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width