1

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 ?

Rajesh
  • 1,514
  • 2
  • 14
  • 15
  • 2
    The second example is NOT equivalent to the other two, without the return statement you are swallowing the "client" and/or "err". – Jake Holzinger Oct 15 '18 at 16:46
  • Also avoid the third snippet with its [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Oct 15 '18 at 17:02
  • really good question I had the same confusion, upvoted – ihor.eth May 12 '21 at 15:58

2 Answers2

3

They are all poor examples.

connect() is thenable, 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.

Mark
  • 90,562
  • 7
  • 108
  • 148
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • "You could rewrite the whole thing as: `let getClient = connect`" This is the best way to summarize it. Or to put it another way, don't create the `getClient` wrapper function because it isn't actually enhancing `connect` in any way. – Damon Oct 15 '18 at 17:23
-1

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));
CRayen
  • 569
  • 2
  • 11