接昨天代码输出题目,
前端面试题之代码输出
前端面试题之代码输出二
前端面试题之代码输出三
前端面试题之代码输出四
前端面试题之代码输出五
异步&事件循环
11.代码输出结果
Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)输出结果如下:
1代码执行顺序如下:
.then或.catch的参数期望是函数,传入非函数则会发生值透传- 第一个
then和第二个then中传入的都不是函数,一个数字,一个对象,因此发生透传。将resolvee(1)的值直接传到最后的then里了。
12.代码输出结果
Promise.reject('err!!!')
.then((res) => {
console.log('success', res)
}, (err) => {
console.log('error', err)
}).catch(err => {
console.log('catch', err)
})输出结果如下:
error err!!!代码执行顺序如下:
.then 函数中的两个参数:
- 第一个参数是用来处理Promise成功的函数
- 第二个则是处理失败的函数
也就是说 Promise.resolve('1') 的值会进入成功的函数,Promise.reject('2') 的值会进入失败的函数。
在这道题中,错误直接被then的第二个参数捕获了,所以就不会被 catch 捕获了










