简体   繁体   English

Promise.reject()与返回promise.reject()

[英]Promise.reject() vs return promise.reject()

I have been trying to understand the difference between the following two and which is the idle way to use: 我一直在尝试理解以下两者之间的区别,这是一种闲置的使用方式:

let getClient = () => {
    return connect()
    .then((client) => {
        return Promise.resolve(client);
    })
    .catch((err) => {
        return Promise.reject(err);
    }
}

and

let getClient = () => {
    return connect()
    .then((client) => {
        Promise.resolve(client);
    })
    .catch((err) => {
        Promise.reject(err);
    }
}

and

let getClient = () => {
    return new Promise((resolve, reject) => {
        return connect()
        .then((client) => {
            resolve(client);
        })
        .catch((err) => {
            reject(err);
        })
    })
}

can someone help me understand the difference? 有人可以帮助我了解其中的区别吗? Does return Promise.resove/reject make a difference to just using Promise.resolve/reject ? 返回Promise.resove / reject是否与仅使用Promise.resolve / reject有所不同?

They are all poor examples. 它们都是不好的例子。

connect() is then able, so it presumably returns a promise. then connect() then可以了,因此大概返回了一个Promise。 Creating extra promises that do nothing except return the results of another promise just over complicates things. 创建额外的promise除了返回另一个promise的结果外什么也不做,这会使事情变得更加复杂。

You could rewrite the whole thing as: 您可以将整个内容重写为:

let getClient = connect;

… and get something that is more-or-less identical (unless you were then going to go and apply some weird edge cases). …并获得几乎相同的东西(除非您随后要去应用一些奇怪的边缘情况)。


Your first example takes the results of resolving the connect promise, creates a new promise and immediately resolves it with the same value, then returns that promise which is adopted by the connect promise. 您的第一个示例采用解析连接承诺的结果,创建一个新的承诺并立即以相同的值对其进行解析,然后返回该连接承诺所采用的那个承诺。

Your second example does the same thing, except without the adoption, so the original connect promise results are available in the next then in the chain. 你的第二个例子做同样的事情,只是没有通过,所以原来的连接承诺的结果是,在未来可then链。 (These results are identical to the ones that stepped through the extra promise in the previous example). (这些结果与上一个示例中逐步实现额外承诺的结果相同)。

Your third example creates a new promise outside the call to connect , and then resolves that with the value from connect . 第三个示例在connect调用之外创建了一个新的Promise,然后使用connect的值来解决它。 It's another pointless extra promise. 这是另一个毫无意义的额外承诺。

A good question. 一个好问题。 In your example, first and last snippet yields the same result. 在您的示例中,第一个和最后一个片段产生相同的结果。 But the second one will give you undefined as you are not returning a promise. 但是第二个将使您不确定,因为您没有兑现承诺。 Also the time taken/sequence of operation differs. 所花费的时间/操作顺序也不同。

 const connect = (input) => new Promise((res, rej) => (!!input ? res('Success') : rej('Failure'))); let getClient_0 = (input) => { return connect(input) .then((client) => { return Promise.resolve(client); }) .catch((err) => { return Promise.reject(err); }) } let getClient_1 = (input) => { return connect(input) .then((client) => { Promise.resolve(client); }) .catch((err) => { Promise.reject(err); }) } let getClient_2 = (input) => { return new Promise((resolve, reject) => { return connect(input) .then((client) => { resolve(client); }) .catch((err) => { reject(err); }) }) } getClient_0(1).then((r) => console.log('getClient_0 -> ',r)).catch(e => console.log('getClient_0 -> ',e)); getClient_0(0).then((r) => console.log('getClient_0 -> ',r)).catch(e => console.log('getClient_0 -> ',e)); getClient_1(1).then((r) => console.log('getClient_1 -> ',r)).catch(e => console.log('getClient_1 -> ',e)); getClient_1(0).then((r) => console.log('getClient_1 -> ',r)).catch(e => console.log('getClient_1 -> ',e)); getClient_2(1).then((r) => console.log('getClient_2 -> ',r)).catch(e => console.log('getClient_2 -> ',e)); getClient_2(0).then((r) => console.log('getClient_2 -> ',r)).catch(e => console.log('getClient_2 -> ',e)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM