When the then method returns a new Promise instance. The catch method cannot catch the error of the new instance

    function loadImageAsync(url) {
      return new Promise(function(resolve, reject) {
        throw new Error("BUG FOREVER")

        const image = new Image();
        image.onload = function() {
          resolve(image);
        };

        image.onerror = function() {
          reject(new Error("Could not load image at " + url));
        };

        image.src = url;
      });
    }

    let src1="https://image.suning.cn/uimg/sop/commodity/213287247648868791262800_x.jpg"
    let src2="https://image.suning.cn/uimg/sop/commodity/742303264420020864606900_x.jpg"

    let img1=loadImageAsync(src1)
    let img2=loadImageAsync(src2)


   img1.then(function(img){
        console.log(" height="+img.height)
        return img2
    }).then(function(img){
        console.log(" width="+img.width)
    }).catch(function(err){
        console.log("fail"+err)
    })

run results

Sep.17,2021

img1 do not leave after throwing an error then directly catch does not go return img2
so your img2 does not define catch


 let img2=loadImageAsync(src2)

promise's constructor is Synchronize's, and as soon as you execute the loadImageAsync method, an exception will be thrown immediately.
however, the error in promise is special, it does not interrupt the running of the following program, and you can still catch it later, for example, you can still catch it after you setTimeout 2s.
I feel that this phenomenon is quite special. I don't know which boss can explain
= split line =
ha. The exception in the constructor should have been eaten by promise and not thrown up, but it will prompt that it has been misled by the console.
let img1=loadImageAsync (src1)
let img2=loadImageAsync (src2)
at this point, the exception has been generated. The status of img1 and img2 is rejected, so they will go straight to catch

.
Menu