简体   繁体   中英

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 ?

They are all poor examples.

connect() is then able, so it presumably returns a promise. Creating extra promises that do nothing except return the results of another promise just over complicates things.

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. (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 . 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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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