0

console.log("Before");

getUser(1)
  .then((user) => getRepositories(user.gitHubUsername))
  .then((repos) => {
    getCommits(repos[0]);
  })
  .then((commits) => {
    console.log("commits: ", commits);
  })
  .catch((err) => console.log("Error: ", err.message));

console.log("After");

function getUser(id) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("Reading a user from a database...");
      resolve({
        id: id,
        gitHubUsername: "mosh"
      });
    }, 2000);
  });
}

function getRepositories(username) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("Calling GitHub API...");
      resolve(["repo1", "repo2", "repo3"]);
    }, 2000);
  });
}

function getCommits(repo) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("Calling GitHub API...");
      resolve("commit");
    }, 2000);
  });
}

now here evey function which is chained is contains a promise which is resolving something i.e username, array etc. but when i run this code on the console it displays commnits : undefined

you can see it in this attached image

Farrukh Normuradov
  • 1,032
  • 1
  • 11
  • 32
MaazAhmad
  • 3
  • 1
  • 3
    `(repos) => {getCommits(repos[0]);}` doesn't return the promise `getCommits()` returns. Either use the `return` keyword or remove the curly brackets: `repos => getCommits(repos[0])` – Ivar Feb 15 '21 at 20:49
  • 2
    Please take a moment to read [ask], especially the part titled "Write a title that summarizes the specific problem". The title is there to get people's attention, not lead into the rest of the question. – Heretic Monkey Feb 15 '21 at 20:53
  • ok. sure I will read how to ask. actually, it was my first question on StackOverflow. thanks guys, found something new to learn – MaazAhmad Feb 15 '21 at 21:13

1 Answers1

0

You missed the return:

    console.log("Before");

     getUser(1)
      .then((user) => getRepositories(user.gitHubUsername))
      .then((repos) => {
        return getCommits(repos[0]);
      })
      .then((commits) => {
        console.log("commits: ", commits);
      })
      .catch((err) => console.log("Error: ", err.message));

    console.log("After");

    function getUser(id) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log("Reading a user from a database...");
          resolve({
            id: id,
            gitHubUsername: "mosh"
          });
        }, 2000);
      });
    }

    function getRepositories(username) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log("Calling GitHub API...");
          resolve(["repo1", "repo2", "repo3"]);
        }, 2000);
      });
    }

    function getCommits(repo) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log("Calling GitHub API...");
          resolve("commit");
        }, 2000);
      });
    }
Farrukh Normuradov
  • 1,032
  • 1
  • 11
  • 32