Error: The `onCancel` handler was attached after the promise settled.
I was just testing against the demo. Sometimes it works, sometimes it throws this error:
```
/Users/songkeys/GitHub/Crossbell-Box/faas/node_modules/p-cancelable/index.js:48
throw new Error('The `onCancel` handler was attached after the promise settled.');
^
Error: The `onCancel` handler was attached after the promise settled.
at onCancel (/Users/songkeys/GitHub/Crossbell-Box/faas/node_modules/p-cancelable/index.js:48:12)
at makeRequest (/Users/songkeys/GitHub/Crossbell-Box/faas/node_modules/got/dist/source/as-promise/index.js:38:13)
at Request.<anonymous> (/Users/songkeys/GitHub/Crossbell-Box/faas/node_modules/got/dist/source/as-promise/index.js:143:17)
at Object.onceWrapper (node:events:628:26)
at Request.emit (node:events:513:28)
at Timeout.retry (/Users/songkeys/GitHub/Crossbell-Box/faas/node_modules/got/dist/source/core/index.js:1278:30)
```
Environment: macOS m1, nodejs v18.
```
demo-app@0.0.1 /Users/songkeys/GitHub/Crossbell-Box/faas
└─┬ html-get@2.9.23
├─┬ got@11.8.5
│ └── p-cancelable@2.1.1 deduped
├── p-cancelable@2.1.1
└─┬ top-sites@1.1.124
└─┬ p-any@3.0.0
├── p-cancelable@2.1.1 deduped
└─┬ p-some@5.0.0
└── p-cancelable@2.1.1 deduped
```
~~I believe this is an issue related to the `got` package. See https://github.com/sindresorhus/got/issues/1489.~~
~~I would also highly recommend deprecating the use of `got` as it's somehow buggy and slow. Instead, use the [`undici`](https://undici.nodejs.org/) package, which would be much more performant. In the future (less than a month later), when nodejs v18 comes out, there is going to be built-in standard `fetch` which is also provided by `undici`.~~
---
edit: After taking another deep look, I think I found the fix...
```diff
+ if (req._isPending) {
onCancel(() => {
debug('fetch:cancel', { url, reflect })
req.cancel()
})
+ }
```
let me know what you think.
关闭于 2023-02-07 7 条评论