打字稿:Promise 的子类/扩展在运行时给出“TypeError: undefined is not a promise" [英] Typescript: subclass/extend of Promise gives 'TypeError: undefined is not a promise' on run time

查看:31
本文介绍了打字稿:Promise 的子类/扩展在运行时给出“TypeError: undefined is not a promise"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试取消我在 Typescript 中的 async 方法调用.

为此,我创建了一个新的 Promise 类型,它继承自 Promise:

class CancelablePromise扩展承诺{公共取消方法:() =>空白;构造函数(执行者:(解析:(值?:T | PromiseLike)=>无效,拒绝:(原因?:任何)=>无效)=>无效){超级(执行者);//显式设置原型.Object.setPrototypeOf(this, CancelablePromise.prototype);}//取消操作公共取消(){如果(this.cancelMethod){this.cancelMethod();}}}

第一个版本没有 Object.setPrototypeOf(..),但推荐使用

我该如何解决这个运行时错误?

<小时>

PS:stackoverflow 上的一个相关错误是 此处,但编译错误并已解决.我已经拆分了编译和运行时错误.

解决方案

我简化了你的例子;这运行没有错误:

interface CancelablePromise扩展 Promise<T>{取消:() =>空白;}类测试{postFileAjax(文件:文件):CancelablePromise{const promiseFunc = (resolve) =>{ 解决() };const promise = new Promise(promiseFunc);const 取消 = () =>{ console.log("取消!") };const cancelablePromise = Object.assign(promise, { cancel });返回可取消的承诺;}}const 测试 = 新测试();const testPromise = test.postFileAjax(null);testPromise.cancel();

TypeScript Playground

  • 我使用了接口继承而不是新类.
  • 我将 cancel 功能直接复制到 promise 上.

I'm trying to cancel my async method call in Typescript.

To do this, I have created a new Promise type, which inherits from Promise:

class CancelablePromise<T> extends Promise<T>{

    public cancelMethod: () => void;
    constructor(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {
        super(executor);
        // Set the prototype explicitly.

        Object.setPrototypeOf(this, CancelablePromise.prototype);

    }

    //cancel the operation
    public cancel() {
        if (this.cancelMethod) {
            this.cancelMethod();
        }
    }
}

The first version was without the Object.setPrototypeOf(..), but it was recommend on the TypeScript GitHub page

When I'm trying to use it, I get a run-time error (no compile errors):

class Test{

    async postFileAjax<T>(file: File): CancelablePromise <T> { 

        var promiseFunc = (resolve) => { resolve() };
        var promise = new CancelablePromise<T>(promiseFunc);
        promise.cancelMethod = () => { console.log("cancel!") };

        return promise;
    }
}

var test = new Test();
test.postFileAjax(null);

Error:

(unknown) Uncaught TypeError: undefined is not a promise
    at CancelablePromise.Promise (<anonymous>)
    at new CancelablePromise (<anonymous>:44:28)
    at __awaiter (<anonymous>:7:12)
    at Test.postFileAjax (<anonymous>:62:16)
    at <anonymous>:75:6
    at HTMLButtonElement.excuteButton.onclick (https://www.typescriptlang.org/play/playground.js:242)

What am I doing wrong? I see that in ES6 you could subclass the Promise (see stackoverflow question), so I would expect it also in TypeScript.

Using Typescript 2.1 and targeting es5

You can see the same result if you run the code in TypeScript Playground, click "run" and check the console (F12) in the new page.

How do I solve this runtime error?


PS: a related error on stackoverflow is here, but that a compile error and solved. I have split the compile and runtime error.

解决方案

I simplified your example; this runs without error:

interface CancelablePromise<T> extends Promise<T> {
    cancel: () => void;
}

class Test {
    postFileAjax<T>(file: File): CancelablePromise<T> {
        const promiseFunc = (resolve) => { resolve() };
        const promise = new Promise<T>(promiseFunc);
        const cancel = () => { console.log("cancel!") };
        const cancelablePromise = Object.assign(promise, { cancel });
        return cancelablePromise;
    }
}

const test = new Test();
const testPromise = test.postFileAjax(null);
testPromise.cancel();

TypeScript Playground

  • I used interface inheritance rather than a new class.
  • I copied the cancel function right onto promise.

这篇关于打字稿:Promise 的子类/扩展在运行时给出“TypeError: undefined is not a promise"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆