I am having the below call which takes a token and makes an API call and returns another token
export default (token, id) => {
if (map[id]) {
return Promise.resolve(map[id]?.token);
}
axios
.post(
`https://**********`,
{ id },
{
headers: {
Authorization: `Bearer ${token}`
}
}
)
.then(response => {
const { token } = response;
map[id] = {
...response,
};
return token;
});
};
for the 1st time, I have 5-10 APIs calling this method which makes the individual token API calls because at the 1st moment there is nothing set in the map. Hence individual token call is made until the map is set. I want to reduce the number of token API calls, i.e. make all the API calls wait until the 1st token API call gets a response and sets it in the map.