Making multiple http requests from a single Axios function

http
axios
promises
async

By making use of Promise.all() it's possible to make multiple async requests.

Promise.all() takes an iterable of promises (ie. async requests)
and returns a single Promise in the form of an array of responses when all requests have resolved.
The responses can therefore be destructured.

Promise.all() is useful where we have multiple related async tasks and all must be completed before the code can move on.

export const getUserAndRepos = async (username) => { const [user, repos] = await Promise.all([ github.get(`/users/${username}`), github.get(`/users/${username}/repos`), ]) return { user: user.data, repos: repos.data } }

In the example above, we make use of an Axios instance that already holds the baseURL & auth headers. See this other note for more details.