Promises eq. syntax

Hello, I am trying to understand what’s going on behind chains of promises’ .then by storing intermediary promises explicitly.

However, in the code below, if the first method getUser() produces a rejection, then, instead of skipping all subsequent .then calls, it produces a Javascript error:

/Users/max/node/async/7-solution-with-promises.js:31
reject(new Error(“=> invalid user”))
^
Error: => invalid user
at Timeout._onTimeout (/Users/max/node/async/7-solution-with-promises.js:31:24)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)

Here is the code. Can anyone explains to me why the code below does not behave correctly, and how to correct it (with intermediary promises still explicit)?

Thanks in advance
–Max

const p1 = getUser(1)
const p2 = p1
.then(function(x) { return getRepositories(x.name) })
.catch(err => console.log(“**Error in getRepositories():”, err.message))
const p3 = p2
.then(function(y) { return getCommits(y.namesList[0]) })
.catch(err => console.log(“**Error in getCommits():”, err.message))
const p4 = p3
.then(function(z) { return console.log(z) })
.catch(err => console.log(“**Error in log():”, err.message))

function getUser(id) {
console.log(“getUser(”, id)
return new Promise( (resolve, reject) => {
// asynchronous function
setTimeout(() => {
const foundUser = {id: id, name: “John”}
if (foundUser.name != “Jane”)
reject(new Error(“=> invalid user”)) // on purpose!
else {
console.log(" => getUser resolved(", foundUser)
resolve(foundUser)
}
}, 2000)
})
}

function getRepositories(user) {
console.log(“getRepositories(”, user)
return new Promise( (resolve, reject) => {
// asynchronous function
setTimeout(() => {
const matchingRepositories = {someData: “n/a”, namesList: [“repo1”, “repo2”, “repo3”]}
if (matchingRepositories.namesList.length != 3) {
reject(new Error(“=> incorrect number of repositories”))
} else {
console.log(" => getRepositories resolved(", matchingRepositories)
resolve(matchingRepositories)
}
}, 2000)
})
}

function getCommits(repository) {
console.log(“getCommits(”, repository)
return new Promise( (resolve, reject) => {
// asynchronous function
setTimeout(() => {
const commits = [“2022”, “2021”, “2020”]
if (commits.length != 3) {
reject(new Error(“=> incorrect number of commits”))
} else {
console.log(" => getCommits resolved(", commits)
resolve(commits)
}
}, 2000)
})
}

I ran your code and got this (which is what I’d expect to get):

getUser( 1
**Error in getRepositories(): => invalid user
**Error in getCommits(): Cannot read properties of undefined (reading 'namesList')
undefined

Here’s your code:

const p1 = getUser(1)
const p2 = p1
  .then(function(x) { return getRepositories(x.name) })
  .catch(err => console.log("**Error in getRepositories():", err.message))
const p3 = p2
  .then(function(y) { return getCommits(y.namesList[0]) })
  .catch(err => console.log("**Error in getCommits():", err.message))
const p4 = p3
  .then(function(z) { return console.log(z) })
  .catch(err => console.log("**Error in log():", err.message))

function getUser(id) {
  console.log("getUser(", id)
  return new Promise( (resolve, reject) => {
    // asynchronous function
    setTimeout(() => {
      const foundUser = {id: id, name: "John"}
      if (foundUser.name != "Jane")
        reject(new Error("=> invalid user")) // on purpose!
      else {
        console.log(" => getUser resolved(", foundUser)
        resolve(foundUser)
      }
    }, 2000)
  })
}

function getRepositories(user) {
  console.log("getRepositories(", user)
  return new Promise( (resolve, reject) => {
    // asynchronous function
    setTimeout(() => {
      const matchingRepositories = {someData: "n/a", namesList: ["repo1", "repo2", "repo3"]}
      if (matchingRepositories.namesList.length != 3) {
        reject(new Error("=> incorrect number of repositories"))
      } else {
        console.log(" => getRepositories resolved(", matchingRepositories)
        resolve(matchingRepositories)
      }
    }, 2000)
  })
}

function getCommits(repository) {
  console.log("getCommits(", repository)
  return new Promise( (resolve, reject) => {
    // asynchronous function
    setTimeout(() => {
      const commits = ["2022", "2021", "2020"]
      if (commits.length != 3) {
        reject(new Error("=> incorrect number of commits"))
      } else {
        console.log(" => getCommits resolved(", commits)
        resolve(commits)
      }
    }, 2000)
  })
}