You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Promise finally方法失效求助:TypeScript引入polyfill仍报错

Troubleshooting "finally is not a function" with Promise.prototype.finally 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 your client.get module). If you import your client library first, then load the polyfill, the Promises returned by client.get were already created before the prototype was patched. Move import '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: run npm ls promise.prototype.finally in your terminal. If it’s missing, install it with npm 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.get returning a native Promise?
    Some libraries return custom "Promise-like" objects instead of standard ES Promises. The polyfill only patches the native Promise.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 polyfilled finally:

    Promise.resolve(client.get(...))
      .then(...)
      .finally(...);
    
  • Check your TypeScript configuration
    If your tsconfig.json has target set to es6 or higher, native Promise should support finally — 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 your lib array includes es2018.promise (or a higher version) to get proper TypeScript type support for finally, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:13:28