一些可能会用到的Node.js面试题

2020-06-17 06:40:46易采站长站整理

.then((x) => { throw new Error('My Error') })
.catch(() => 1)
.then((x) => x + 1)
.then((x) => console.log(x))
.catch(console.error)

答案是2,逐行解释如下:

创建新的Promise,resolve值为1。
x为1,加1之后返回2。
x为2,但是没有用到。抛出一个错误。
捕获错误,但是没有处理。返回1。
x为1,加1之后返回2。
x为2,打印2。
不会执行,因为没有错误抛出。

英文: Node.js Interview Questions and Answers (2017 Edition)

译者: Fundebug