how to return objects asynchronously after fetching data in loop?

Use Promise.all

const requestSideBarData = async(state, request) => {

    let newState = state;
    const promises = [];
    Object.keys(newState.data).forEach(element => {
        promises.push(axios.post(`${ApiEndPoints.getDropDownHeaders}${element}`,request))
    });
    const results = await Promise.all(promises);
    var index = 0;
    Object.keys(newState.data).forEach(element => {
        newState.data[element] = results[index++].data;
    })
    return newState;

};

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top