第 1 题
- var p = new Promise((resolve, reject) => {
- reject(Error('The Fails!'))
- })
- p.catch(error => console.log(error.message))
- p.catch(error => console.log(error.message))
输出结果是什么?
A. 打印一次'The Fails!'
B. 打印两次'The Fails!'
C.
UnhandledPromiseRejectionWarning
D. 进程退出
例子中用构造函数创建了一个 Promise, 然后用 reject 回调立即触发一个错误..catch 处理函数跟 DOM 的. addEventListener(event, callback) 或者 Event Emitter 中的. on(event, callback) 类似, 可以添加多个回调函数. 因此, 每个回调函数都会被调用, 接收的参数也是一样的.
答案: B
第 2 题
- var p = new Promise((resolve, reject) => {
- return Promise.reject(Error('The Fails!'))
- })
- p.catch(error => console.log(error.message))
- p.catch(error => console.log(error.message))
输出结果是什么?
A. 打印一次'The Fails!'
B. 打印两次'The Fails!'
C.
UnhandledPromiseRejectionWarning
D. 进程退出
如果使用构造函数创建 Promise, 必须调用 resolve 或者 reject . 这里在构造函数中返回了一个新的 Promise, 外层的 Promise 并没有用到它的返回值, 内部的 Promise 后面没有. catch, 因此答案是 UnhandledPromiseRejectionWarning
答案: C
第 3 题
- var p = new Promise((resolve, reject) => {
- reject(Error('The Fails!'))
- })
- .catch(error => console.log(error))
- .then(error => console.log(error))
输出结果是什么?
A. 打印错误和 undefined
B. 打印两次错误
C.
- UnhandledPromiseRejectionWarning
- D. undefined
当链式调用. then 和. catch 时, 可以看成是一系列的步骤. 每个 .then 接收前一个. then 的返回值作为参数. 但是, 如果某一步碰到错误时, 后续的. then 都会被跳过, 直到碰到一个. catch. 如果要重写某个错误, 只要返回一个非错误值就行了. 这样它就可以被后续的. then 获取到.
提示: console.log() 总是返回 undefined.
答案: A
第 4 题
- var p = new Promise((resolve, reject) => {
- reject(Error('The Fails!'))
- })
- .catch(error => console.log(error.message))
- .catch(error => console.log(error.message))
输出结果是什么?
A. 打印一次错误信息
B. 打印两次错误信息
C.
UnhandledPromiseRejectionWarning
D. 进程退出
链式调用. catch 时, 每个. catch 只处理前面的. then 或. catch 抛出的错误. 这个例子中, 第一个. catch 返回值是 console.log, 这个值只有在两个. catch 后加一个. then 才能拿到.
答案: A
第 5 题
- new Promise((resolve, reject) => {
- resolve('Success!')
- })
- .then(() => {
- throw Error('Oh noes!')
- })
- .catch(error => {
- return "actually, that worked"
- })
- .catch(error => console.log(error.message))
输出结果是什么?
- A. print message once
- B. print message twice
C.
- UnhandledPromiseRejectionWarning
- D. nothing prints
.catch 可以通过返回常规值来忽略错误. 这里如果再接一个. then 就能获取前面的返回值了.
答案: D
第 6 题
- Promise.resolve('Success!')
- .then(data => {
- return data.toUpperCase()
- })
- .then(data => {
- console.log(data)
- })
A. 打印 "Success!" 和 "SUCCESS!"
B. 打印 "Success!"
C. 打印 "SUCCESS!"
D. 不打印
这题比较简单,.then 的返回值会挨个传递给后续的. then, 注意一定要 return.
答案: C
第 7 题
- Promise.resolve('Success!')
- .then(data => {
- return data.toUpperCase()
- })
- .then(data => {
- console.log(data)
- return data
- })
- .then(console.log)
- A. print "SUCCESS!"
- B. print "Success!"
- C. print "SUCCESS!" and "SUCCESS!"
- D. nothing prints
跟前面那道题一样, 这里 console.log 会调用两次.
答案: C
第 8 题
- Promise.resolve('Success!')
- .then(data => {
- data.toUpperCase()
- })
- .then(data => {
- console.log(data)
- })
- A. print "SUCCESS!"
- B. print "Success!"
- C. print "SUCCESS!" and "SUCCESS!"
- D. prints undefined
这里第一个. then 没有返回, 所以后面的. then 接收不到值.
答案: D
第 9 题
- Promise.resolve('Success!')
- .then(() => {
- throw Error('Oh noes!')
- })
- .catch(error => {
- return 'actually, that worked'
- })
- .then(data => {
- throw Error('The fails!')
- })
- .catch(error => console.log(error.message))
- A. print "Oh noes!" and "The fails!"
- B. print "Oh noes!"
- C. print "The fails!"
- D. print "actually, that worked"
- E. nothing prints
很明显, 第一个错误已经被第一个. catch 处理了, 所以第二个. catch 捕获的是它前面的错误.
答案: C
来源: http://www.jianshu.com/p/a80edd807ad8