Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
How can I convert this async function to promise?

Hello I have this async function which retrieves user profile and repositories through github api and returns them in an object.

And I want to convert this to a promise based function by using promise chaining (without any helper methods).

async function getUser(user) {
  const profileResponse = await fetch(`https://api.github.com/users/${user}`);
  const profileData = await profileResponse.json();

  const repoResponse = await fetch(`https://api.github.com/users/${user}/repos`);
  const reposData = await repoResponse.json();
  // returning an object with profile and data
  return {
    profile:profileData,
    repos:repoData,
  };
}

//Calling the function here.
getUser("abufattah").then((res) => console.log(res));

I have managed to get it done using two helper functions and promise.all() method.

But how can I achieve the same thing by using promise chaining without any helper functions.

//Helper function 1: returns a promise with user profile data.
function getUserProfile(user) {
  return fetch(`https://api.github.com/users/${user}`)
         .then((res) =>res.json());
}


//Helper function 2: returns a promise with user repositories data.
function getUserRepos(user) {
  return fetch(`https://api.github.com/users/${user}/repos?per_page=5&sort=created`)
         .then((res) => res.json());
}
//Main function
function getUserWithPromise(user) {
  return new Promise((resolve) => {
    let profile = getUserProfile(user);
    let repos = getUserRepos(user);

    Promise.all([profile, repos]).then((values) => {
      resolve({ profile: values[0], repos: values[1] });
    });
  });
}

// calling the function here
getUserWithPromise("abufattah").then((res) => console.log(res));
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can:

//Main function
function getUserWithPromise(user) {
  return Promise.all([
    fetch(`https://api.github.com/users/${user}`).then((res) =>res.json()),
    fetch(`https://api.github.com/users/${user}/repos?per_page=5&sort=created`).then((res) => res.json())
  ]).then(([result1, result2]) => ({ profile: result1, repos: result2 }));
}

// calling the function here
getUserWithPromise("abufattah").then((res) => console.log(res));

Chain:

function getUserWithPromise(user) {
  return fetch(`https://api.github.com/users/${user}`)
    .then((res) => {
      return fetch(`https://api.github.com/users/${user}/repos?per_page=5&sort=created`).then((fetch2Result) => ([res.json(), fetch2Result.json()]))
    }).then(([result1, result2]) => ({ profile: result1, repos: result2 }))
}

// calling the function here
getUserWithPromise("abufattah").then((res) => console.log(res));
about 4 years ago · Juan Pablo Isaza Report

0

Transformation of async/await syntax to .then() calls is quite mechanical, especially if it doesn't involve any control flow syntax (loops or conditionals):

function getUser(user) {
  return fetch(`https://api.github.com/users/${user}`).then(profileResponse => {
    return profileResponse.json().then(profileData => {
      return fetch(`https://api.github.com/users/${user}/repos`).then(repoResponse => {
        return repoResponse.json().then(reposData => {
          // returning an object with profile and data
          return {
            profile:profileData,
            repos:repoData,
          };
        });
      });
    });
  });
}

But there's no good reason to write code like that. If it's just that your target environment does not support async/await, let a transpiler do the transformation.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!