问答题897/1620【Promise第14题】下面代码的输出是什么?

1const promise = new Promise((resolve, reject) => { 2 reject("error"); 3 resolve("success2"); 4}); 5promise 6.then(res => { 7 console.log("then1: ", res); 8 }).then(res => { 9 console.log("then2: ", res); 10 }).catch(err => { 11 console.log("catch: ", err); 12 }).then(res => { 13 console.log("then3: ", res); 14 })
难度:
2022-01-09 创建

参考答案:

解析

catch不管被连接到哪里,都能捕获上层未捕捉过的错误。

至于then3也会被执行,那是因为catch()也会返回一个Promise,且由于这个Promise没有返回值,所以打印出来的是undefined。

结果

"catch: " "error"
"then3: " undefined

最近更新时间:2022-01-09

赞赏支持

题库维护不易,您的支持就是我们最大的动力!