If Promise object rejects itself - Stack Overflow
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-06-14 10:53:56
0
2
741

Code:

            var der = {};

            var p = new Promise(function (resolve, reject) {
                der.reject = reject;
            })
            p.then((v) => {
                console.log('resolve');
                console.log(v);
            })
            p.catch(function (v) {
                console.log(v);
                console.log('reject');
                console.log(v);
            });

            der.reject(p);
            console.log(p);

Only reject is output under Firefox, input normally under Chrome, and then Uncaught (in promise) will appear

Excuse me, why does an uncaught exception occur? The catch of p is clearly defined, and why does Firefox only output reject and the operation of outputting p has no effect (in fact, it should be caused by an uncaught exception)


Supplement:
The reason for the uncaught exception is that although the second function is not defined in then, then is still run, and the cause and status are passed to the returned new Promise object. The Promise object does not have a catch callback, so an exception is reported
But the reason why the output p in Firefox has no effect is still unclear

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(2)
伊谢尔伦
var der = {};

var p = new Promise(function (resolve, reject) {
    der.reject = reject;
})
// 下面修改成这个样子,因为then内部的处理函数也需要异常捕获
p.then((v) => {
    console.log('resolve');
    console.log(v);
}).catch(function (v) {
    console.log(v);
    console.log('reject');
    console.log(v);
});

der.reject(p);
console.log(p);
学霸

Promise is equivalent to a standard. Any prototype that conforms to the standard can be called a promise. Different browsers may have different features outside the standard. Obviously, Firefox believes that .catch and .then can be declared separately, and then at runtime Response, that is, no error is reported as you mentioned above; and Google believes that .catch should be handled together with the exception in .then, so it needs to be applied to the promise after .then. If you use .then().catch together, Google will not report an error. The underlying reason may be that one is based on events and the other is based on polling.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template