Promise finally方法失效求助:TypeScript引入polyfill仍报错
Hey there, let's break down why your finally method is throwing that error even after importing the polyfill. Here are the most likely culprits and how to debug them:
Check the import order (critical!)
The polyfill needs to run before any code that creates or uses Promises (including yourclient.getmodule). If you import your client library first, then load the polyfill, the Promises returned byclient.getwere already created before the prototype was patched. Moveimport 'promise.prototype.finally'to the absolute top of your entry file (index.js/ts) — even above other imports — to ensure it runs first.Verify the polyfill is actually loading
First, double-check you’ve installed the package: runnpm ls promise.prototype.finallyin your terminal. If it’s missing, install it withnpm install promise.prototype.finally --save.
Then, add a quick debug log right after the import:import 'promise.prototype.finally'; console.log('Promise.prototype.finally exists:', typeof Promise.prototype.finally === 'function');If this logs
false, the polyfill isn’t loading correctly — check for typos in the import path or conflicts with other Promise-related polyfills.Is
client.getreturning a native Promise?
Some libraries return custom "Promise-like" objects instead of standard ES Promises. The polyfill only patches the nativePromise.prototype, so it won’t affect these custom implementations. Test this with:const responsePromise = client.get(...); console.log('Is native Promise:', responsePromise instanceof Promise);If this logs
false, wrap the result in a native Promise to access the polyfilledfinally:Promise.resolve(client.get(...)) .then(...) .finally(...);Check your TypeScript configuration
If yourtsconfig.jsonhastargetset toes6or higher, native Promise should supportfinally— but if you’re running in an older environment (like Node.js < 10 or legacy browsers), the native implementation might still be missing. Ensure your polyfill is loaded before the runtime executes any Promise code.
Also, confirm yourlibarray includeses2018.promise(or a higher version) to get proper TypeScript type support forfinally, though this won’t fix the runtime error directly.Avoid redundant imports
You mentioned importing the polyfill multiple times. While this shouldn’t break things, it can sometimes lead to unexpected loading order issues. Try removing all duplicate imports and keep only the top-level one in your entry file to simplify debugging.
内容的提问来源于stack exchange,提问作者Kousha

