承诺覆盖会导致错误 [英] Promise override causes then calls to error

查看:38
本文介绍了承诺覆盖会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码.

class test extends Promise {
    constructor(executor) {
        super(executor)
    }

    static getPromise() {
        return new test((res, rej) => {
            res(true)
        })
    }
}

let t = test.getPromise()
t.then(value => {console.log("Works")}, 
       value => {console.log("Works")})

这很好用,但是如果我直接将相同的函数放到上级函数中,则会引发错误.

this works just fine, but if I were to put the same function directly into the super function, it throws an error.

class test extends Promise {
    constructor(executor) {
        super((res, rej) => {
            res(true)
        })
    }

    static getPromise() {
        return new test((res, rej) => {
            res(true)
        })
    }
}

let t = test.getPromise()
t.then(value => {console.log("Works")}, 
       value => {console.log("Works")})

错误是 TypeError:Promise解析或拒绝函数不可调用为什么会发生这种情况,这两段代码不是等效的吗?

the error is TypeError: Promise resolve or reject function is not callable Why is this happening, aren't these two pieces of code equivalent?

我的目标是通过简单地拥有以下内容来简化库的使用:

My goal is to simplify use of my library by simply having:

let t = new test()
t.then(value => {console.log("Works")}, 
       value => {console.log("Works")})

我知道这不是一个简单的简化,我可以保留静态函数(如果需要),但是我对此感到好奇,为什么会不起作用.

I understand this is not much of a simplification, I am ok with keeping the static function if required, but I am more curious as to why this is not working.

我的问题是:

  1. 为什么这些不相等,为什么会出错?
  2. 我如何按预期进行这项工作?

感谢Bergi,我意识到我要重写的Promise类正在将测试构造函数用于"then"功能.当我假设它正在使用原始构造函数时.事后看来,这很明显,但是我的解决方法最终只是将其添加到构造函数中.

Thanks to Bergi, I came to the realization that the promise class I am overriding is using the test constructor for the "then" functions. While I was assuming it was using the original constructor. It is obvious in hindsight, but my fix ended up being simply adding this to the constructor.

if(!executor) executor = (res, rej) => { res(true) }

推荐答案

为什么这些不相等,为什么会出错?

Why are these not equivalent, and why is this erroring?

您被重写的构造函数永远不会调用 executor 并将其promise解析函数( resolve reject )传递给它.它们很重要!您不能将 Promise 子类化,并不能像这样更改界面.

Your overridden constructor does never call the executor and pass it the promise resolving functions (resolve and reject). They are important! You cannot subclass Promise and change the interface like that.

为什么在调用不使用构造函数的 new test()时会引发错误?它不是.当您调用 t.then(…)时,它将引发错误,该尝试尝试构造一个新的Promise(作为其返回值)并为此使用 t 的构造函数.它确实传递了适当的执行程序回调,并且希望它与 resolve reject 同步调用,而您的代码不会这样做.通话结束后,它为没有两个功能而苦恼.

Why does this throw an error when you call new test() that doesn't use the constructor? It does not. It throws the error when you call t.then(…), which tries to construct a new promise (for its return value) and uses t's constructor for that. It does pass a proper executor callback, and expects it to be called synchronously with resolve and reject - which your code does not do. After the call, it bitches about not having got two functions.

我如何按预期进行这项工作?

How can I make this work as I expect?

不要使用promise子类化.没有任何理由.您只想拥有一个返回诺言的普通函数.使用

Don't use promise subclassing. There's no reason for it. You just want to have a normal function that returns a promise. Use

const test = {
    getPromise() {
        return Promise.resolve(true)
    }
};
test.getPromise().then(…);

function test() {
    return Promise.resolve(true);
}
test().then(…);

这篇关于承诺覆盖会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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