Title: `AsyncResource.bind` always alters the value of `this` unexpectedly · Issue #42158 · nodejs/node · GitHub
Open Graph Title: `AsyncResource.bind` always alters the value of `this` unexpectedly · Issue #42158 · nodejs/node
X Title: `AsyncResource.bind` always alters the value of `this` unexpectedly · Issue #42158 · nodejs/node
Description: Version 17.6.0 Platform No response Subsystem No response What steps will reproduce the bug? const fn = AsyncResource.bind(function () { console.log(this) }) fn.apply('foo') // will log the AsyncResource instead of 'foo' const obj = { fn...
Open Graph Description: Version 17.6.0 Platform No response Subsystem No response What steps will reproduce the bug? const fn = AsyncResource.bind(function () { console.log(this) }) fn.apply('foo') // will log the AsyncRe...
X Description: Version 17.6.0 Platform No response Subsystem No response What steps will reproduce the bug? const fn = AsyncResource.bind(function () { console.log(this) }) fn.apply('foo') // will log the...
Opengraph URL: https://github.com/nodejs/node/issues/42158
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`AsyncResource.bind` always alters the value of `this` unexpectedly","articleBody":"### Version\r\n\r\n17.6.0\r\n\r\n### Platform\r\n\r\n_No response_\r\n\r\n### Subsystem\r\n\r\n_No response_\r\n\r\n### What steps will reproduce the bug?\r\n\r\n```js\r\nconst fn = AsyncResource.bind(function () {\r\n console.log(this)\r\n})\r\n\r\nfn.apply('foo') // will log the AsyncResource instead of 'foo'\r\n\r\nconst obj = { fn }\r\n\r\nobj.fn() // will log the AsyncResource instead of `obj`\r\n```\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nAlways as the code is explicitly binding `thisArg` even when not provided.\r\n\r\n### What is the expected behavior?\r\n\r\n`AsyncResource.bind` should use the `this` value provided by the caller by default and only bind `this` when a `thisArg` is passed explicitly.\r\n\r\n### What do you see instead?\r\n\r\n`AsyncResource.bind` defaults to setting `this` to itself, which is not only unexpected in pretty much all cases, but also makes it impossible to use the `this` provided by the caller without an explicit reference to the caller at bind time. This means that in all situations right now it would make more sense to use `runInAsyncScope` instead of `bind` making it effectively unusable.\r\n\r\n### Additional information\r\n\r\nAs an APM vendor, we're making heavy use of this API and there is not a single occurrence in our code where the default works.\r\n\r\nThe feature was originally implemented in #36782","author":{"url":"https://github.com/rochdev","@type":"Person","name":"rochdev"},"datePublished":"2022-03-01T02:08:15Z","url":"https://github.com/nodejs/node/issues/42158"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:e97df20a-65c4-c26a-ad98-3a182978cf51 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B61E:334404:AD9AFC:E742FD:6AED3518 |
| html-safe-nonce | 0023cff72dd41e13b7137af27d1abfb28ae7c267724456885788c6c21ba45a67 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjFFOjMzNDQwNDpBRDlBRkM6RTc0MkZEOjZBRUQzNTE4IiwidmlzaXRvcl9pZCI6IjEwODM1NTc1MTM5ODc0Mzc4NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 325697d8d57646f37ea4a0317b1c12814dbe7ffcfb68258a7da1f973cd1d8b48 |
| hovercard-subject-tag | issue:1154764935 |
| 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/nodejs/node/42158/issue_layout |
| twitter:image | https://opengraph.githubassets.com/994afa5142efecb0789e9d769380361bcd547a86d28154c34e10b153afc8f278/nodejs/node/issues/42158 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/994afa5142efecb0789e9d769380361bcd547a86d28154c34e10b153afc8f278/nodejs/node/issues/42158 |
| og:image:alt | Version 17.6.0 Platform No response Subsystem No response What steps will reproduce the bug? const fn = AsyncResource.bind(function () { console.log(this) }) fn.apply('foo') // will log the AsyncRe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rochdev |
| hostname | github.com |
| expected-hostname | github.com |
| None | e51d0b7b5ab34b1688c05b54c3ed17b8658b2fa9bca973a9c740683ccb12cf4a |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| 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 | 59ebb7b901cc154ecc179a44f266fcf69f343b30 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width