The function of this.constructor! = = Promises in promise source code

after reading the source code core.js of promise, I really don"t quite understand the this.constructor! = = Promises in the code.


Promise.prototype.then = function(onFulfilled, onRejected) {
    if (this.constructor !== Promises) {
        return safeThen(this, onFulfilled, onRejected);
    }
    var res = new Promises(noop);
    handle(this, new Handler(onFulfilled, onRejected, res));
    return res;
};

function safeThen(self, onFulfilled, onRejected) {
    return new self.constructor(function (resolve, reject) {
        var res = new Promises(noop);
        res.then(resolve, reject);
        handle(self, new Handler(onFulfilled, onRejected, res));
    });
}
this.constructor !== Promises

this new promise can be read. Then this this must all be this instance
how can I execute the code in the safeThen function

Apr.20,2022

looks at the safeThen method. The constructor of this.constructor should be the same as the Promsie constructor, receiving a method and passing in the resolve,reject parameter.
so this.constructor! = = Promises is to judge, this. Whether constructor inherits Promsie.


is used to handle situations when other objects refer to Promise's then method through call and apply?

Menu