4

I am trying to call two apis with two async function.The thing is that the second
async function's api call depends upon the the result of the first async function.

  1. My first async function fetches some data from an api and returns an
    array(users) of objects .
  2. The second async function calls another api that takes the id property from
    each object of users as url parameter.and will returns a status for each
    object and we want to filter only those user that has status 204.

I know the logic on how to do this but can't implement it in real world.Plus
nested async functions always seems hard to me , here is my attempt :

    const getUsers = async () => {
        const { users } = await axios.get(url);
        return users;
    };

    const getCheckedUsers = async () => {
        const allUsers = await getUsers();
        const promises = allUsers.then(users => {
            users.map(async user => {
                const { status } = await axios.get(another_url_that_takes_`user.id`);
                if (status === "204") {
                    return user;
                }
            });
        });
        const results = await Promise.all(promises);
        return results;
    }

First function works fine i tested it separately.It returns the needed array.
But the issue starts when i try to combine first async function with the second
one.

3
  • 1
    What's the result of your code ? Commented Oct 23, 2019 at 12:56
  • @YoussefTounoussi result is unhandled promise rejection Commented Oct 23, 2019 at 13:03
  • So return allUsers and not results and remove this line const results = await Promise.all(promises); Commented Oct 23, 2019 at 13:08

2 Answers 2

1

Using async/await there's no need to get lost with Promises since the code just runs line by line:

const getUsers = async () => {
  const { users } = await axios.get(url);
  return users;
};

const getCheckedUsers = async () => {
  const allUsers = await getUsers();
  const results = [];
  for (user of allUsers) {
    const { status } = await axios.get(`another_url_that_takes_${user.id}`);
    if (status === "204") {
      results.push(user);
    }
  }
  return results;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Wouldn't it return the results[] even before the data is fetched ?
No, since the await inside the for..of ensures the synchronous behavior for each iteration.
Seems to be working fine for now . Thanks but i will still try do some more tests with this and will come back if any issue comes up .
@ErickPetrucelli I know this answers the question. I simply want to point out the behaviour difference to unaware readers.
@Rocktim I'm happy it worked. Please don't forget to mark the accepted answer if you think your problem is solved at all.
|
1

There are a few issues with your current code.

  1. You invoke .then on a non promise value (allUsers.then). This isn't needed because you already do const users = await getUsers() which will set users to the resolved value, not a promise.

  2. By using map and only returning the user if status equals "204", you will implicitly return undefined for users that have a status other then "204". If the intent is to leave out the values completely you can use filter on the users array.

const getUsers = async () => {
    const { users } = await axios.get(url);
    return users;
};

const getStatus = async (user) => {
    const { status } = await axios.get(`another_url_that_takes_${user.id}`);
    return status;
};

const getCheckedUsers = async () => {
    const users = await getUsers();
    const statuses = await Promise.all(users.map(getStatus));
    return users.filter((_, index) => statuses[index] === "204");
};

2 Comments

thanks for the explanation :) I really learned a lot from this.
This explanation looking like a wowww.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.