Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
  • Promise

Promise

promise 的三种状态

pengding

resolved

rejected

then 和 catch 改变状态

then 正常返回 resolved, 里面报错返回 rejected

catch 正常返回 resolved,里面报错返回 rejected

eg: 1.

Promise.resolve().then(() => {
  console.log(1)
}).catch(() => {
  console.log(2)
}).then(() => {
  console.log(3)
})

答案:

1,3

解析:

then 里面没有报错,所以不会走catch方法,直接走then方法。打印1,3

Promise.resolve().then(() => {
  console.log(1)
  throw new Error('error1')
}).catch(() => {
  console.log(2)
}).then(() => {
  console.log(3)
})

答案:

1,2,3

解析:

then 里面有报错,返回rejected,所以会走catch方法,catch里面没有报错,返回resolved,会继续走走then方法。所以打印1,2,3

Promise.resolve().then(() => {
  console.log(1)
  throw new Error('error1')
}).catch(() => {
  console.log(2)
}).catch(() => {
  console.log(3)
})

答案:

1,2

解析:

then 里面有报错,返回rejected,所以会走catch方法,catch里面没有报错,返回resolved,所以不会走catch方法,所以打印1,2

未完待续

Edit this page
最近更新: 2025/12/2 01:46
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人