Title: Async computed signal - Add the ability to easily create async signals with dependencies · Issue #54032 · angular/angular · GitHub
Open Graph Title: Async computed signal - Add the ability to easily create async signals with dependencies · Issue #54032 · angular/angular
X Title: Async computed signal - Add the ability to easily create async signals with dependencies · Issue #54032 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Currently, the computed operator for signal provides the ability to create a new signal value, which is dependent on other signals. The limitation ...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Currently, the computed operator for signal provides the ability to create a new signal value, which is dep...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Currently, the computed operator for signal provides the ability to create a new signal value, which is dep...
Opengraph URL: https://github.com/angular/angular/issues/54032
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Async computed signal - Add the ability to easily create async signals with dependencies","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncore\n\n### Description\n\nCurrently, the computed operator for signal provides the ability to create a new signal value, which is dependent on other signals.\r\n\r\nThe limitation I want to solve is that currently, the computed operator only allows for synchronous computations with immediate resolution of the newly computed value.\r\n\r\nMy suggestion is to allow computed signals to accept an async value (promise or observable), which when combined with a flag would wait until an actual value returns from the async value before updating the signal.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\n\n### Proposed solution\n\nUse case - \r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n}\r\n```\r\nConsider the use case in the component above, where on every keystroke I want to fetch some data from a backend service.\r\n\r\nExample - Promise:\r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n\t\r\n\tservice = inject(DataService);\r\n\t\r\n\tdata:Signal\u003cData | undefined> = computed(\r\n\t () => this.service.queryAsPromise({query: this.query()}), // returns a promise \r\n\t\t{ async: true }\r\n\t)\r\n}\r\n```\r\n\r\nExample - Observable:\r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n\t\r\n\tservice = inject(DataService);\r\n\t\r\n\tdata:Signal\u003cData | undefined> = computed(\r\n\t\t() => this.service.queryAsObservable({query: this.query()}), // returns an observable\r\n\t\t{ async: true }\r\n\t)\r\n}\r\n```\r\n\r\nUsing the computed operator with an async callback, but without specifying the async flag would result in a signal that holds an async value (same behavior today so not a breaking change):\r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n\t\r\n\tservice = inject(DataService);\r\n\t\r\n\tdata:Signal\u003cPromise\u003cData>> = computed(\r\n\t\t() => this.service.queryAsPromise({query: this.query()}), \r\n\t)\r\n}\r\n```\n\n### Alternatives considered\n\nCurrently this is possible only with these 2 options:\r\n1 - Using promise and effect which writes to a new signal:\r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n\t\r\n\tdata = signal([]);\r\n\r\n\tconstructor(service:DataService) {\r\n\t\teffect(async () => {\r\n\t\t\tconst query = this.query()\r\n\t\t\tconst result = await service.queryAsPromise({query});\r\n\t\t\tthis.data.set(result);\r\n\t\t}, { allowSignalWrites: true })\r\n\t}\r\n}\r\n```\r\n\r\n2 - Using observable and jumping back and forwards from signal and observables to merge all required data (not to mention the added complexity if you have several dependencies for your async request):\r\n```\r\n@Component({\r\n\ttemplate: `\r\n\t\u003cinput (change)=\"query.set($event.value)\">\r\n\t`,\r\n})\r\nclass TestComponent {\r\n\tquery = signal('');\r\n\t\r\n\tservice = inject(DataService);\r\n\t\r\n\tdata = toSignal(\r\n\t\ttoObservable(this.query)\r\n\t\t\t.pipe(mergeMap(query => this.service.queryAsObservable({query})))\r\n\t);\r\n}\r\n```\r\n\r\nBoth solutions are very verbose, convoluted, and seems like bending the framework to do something it wasn't supposed to in terms of ergonomics (my opinion).\r\nThe usecase seems pretty trivial, and I come across it constantly in our projects, so much so that I created a new function just to wrap this logic (toSignalWithDeps).","author":{"url":"https://github.com/rezoled","@type":"Person","name":"rezoled"},"datePublished":"2024-01-23T11:26:42Z","url":"https://github.com/angular/angular/issues/54032"}
| 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:67807909-5f9e-08cf-6f26-439ec36a638f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D76:DA744:2CF5BAE:3C808A6:6B01E2E1 |
| html-safe-nonce | a2f750afaeed12bc80f520644fb745e3071583322a68eb7de707d06b56bb2427 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDc2OkRBNzQ0OjJDRjVCQUU6M0M4MDhBNjo2QjAxRTJFMSIsInZpc2l0b3JfaWQiOiI2MDYyNzYwNTU5NjMwMjEzODU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4e74c7dbc61e196892f57e2113cbcb93be8db3d82e45d123825aac3064efc617 |
| hovercard-subject-tag | issue:2095863002 |
| 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/54032/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c1583739e857b67764125193143179f615370df9abd488c6ef2f9af8d6ce1d30/angular/angular/issues/54032 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c1583739e857b67764125193143179f615370df9abd488c6ef2f9af8d6ce1d30/angular/angular/issues/54032 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Description Currently, the computed operator for signal provides the ability to create a new signal value, which is dep... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rezoled |
| 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