看一下如下幾個單選題,正確的輸出是什麼呢? 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
經典的鏈式調用和返回值傳遞