这几个Promise的输出到底是?

看一下以下几个单选题,正确的输出是什么呢?

 

(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. print message once
B. print message twice
C. Unhandled Promise Rejection Warning
D. process exits

答案:B
我们使用构造函数方法创建一个Promise实例,立即使用 reject 回调触发一个错误。
catch处理程序的工作方式类似于DOM的 .addeventlistener(事件、回调)或事件发射器的 .on(事件、回调),其中可以添加多个回调。每个回调都具有相同的参数。

 

(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。

 

(3)

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。

 

(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 来访问。

 

(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. 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 被调用。

广州设计公司https://www.houdianzi.com 我的007办公资源网站https://www.wode007.com

(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
经典的链式调用和返回值传递

posted @ 2020-10-20 16:17  笑人  阅读(280)  评论(0编辑  收藏  举报