看一下如下几个单选题,正确的输出是什么呢? javascript
(1)java
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. print message once
B. print message twice
C. Unhandled Promise Rejection Warning
D. process exits
答案:B
咱们使用构造函数方法建立一个Promise实例,当即使用 reject 回调触发一个错误。
catch处理程序的工做方式相似于DOM的 .addeventlistener(事件、回调)或事件发射器的 .on(事件、回调),其中能够添加多个回调。每一个回调都具备相同的参数。 promise
(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. print message once
B. print message twice
C. Unhandled Promise Rejection Warning
D. process exits
答案:C
使用Promise构造函数时,必须调用 resolve() 或 reject() 回调,建立的实例才能有效的执行 .then 或 .catch。 ui
(3).net
var p = new Promise((resolve, reject) => { reject(Error('The Fails!')); }) .catch(error => console.log(error)) .then(error => console.log(error));
A. print error and `undefined`
B. print error twice
C. Unhandled Promise Rejection Warning
D. undefined
答案:A
第一个 console.log 打印出 reject() 抛出的错误,接着它返回了 undefined,并将其传递给了 .then。 code
(4)事件
var p = new Promise((resolve, reject) => { reject(Error('The Fails!')); }) .catch(error => console.log(error.message)) .catch(error => console.log(error.message));
A. print error message once
B. print error message twice
C. Unhandled Promise Rejection Warning
D. process exits
答案:A
当链接 .catch 时,每个都只处理前面的 .then 或 .catch 中抛出的错误。例子中,第一个 .catch 返回 console.log,它只能在后续的链式调用中添加一个 .then 来访问。 ip
(5)get
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. Unhandled Promise Rejection Warning
D. nothing prints
答案:D
.catch 能够经过返回一个常规值来忽略(或覆盖)错误。此技巧仅在后续的 .then 接收值时才有效。
(6)
Promise.resolve('Success!') .then(data => { return data.toUpperCase(); }) .then(data => { console.log(data); });
A. print "Success!" and "SUCCESS!"
B. print "Success!"
C. print "SUCCESS!"
D. nothing prints
答案:C
Promise的链式调用是按顺序传递数据,从返回值传递到下一个。return 关键字的做用是将一个值传递给下一个。
(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
答案:C
有两个 console.log 被调用。
(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`
答案:D
第一个 .then 没有 return 任何内容,默认返回 undefined。
(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
答案:C
经典的链式调用和返回值传递