javascript - A question about promise exception catching, please give me an answer
过去多啦不再A梦
过去多啦不再A梦 2017-06-12 09:28:47
0
1
589

I am new to promises and there is something I don’t understand. I would like to ask you for help. :

The code is as follows. My question is why I hand-written an undefined error in the timer, which will cause the console to directly report an error, instead of saying rejection and outputting 2? And if I reject() directly , the console will not report an error, and it is normal if you write by hand outside instead of inside the timer. (Here b is an undefined variable)

var p = new Promise(function(resolve, reject) { setTimeout(function () { b++; },1000); //b++; }); p.then(function(){ console.log(1); },function(){ console.log(2); });

According to the above, the console reports an error.

According to the following words, the rejection will be captured and output 2

var p = new Promise(function(resolve, reject) { b++; }); p.then(function(){ console.log(1); },function(){ console.log(2); });
过去多啦不再A梦
过去多啦不再A梦

reply all (1)
黄舟

Because the b++ error is in the setTimeout function, not in the promise function.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!