Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why my promise all function returns anonymous function?

I have two promises making an api call , both should return an array of data , I’ve tested the api locally with Postman , it works fine , but executing those two promises with Promise All it display on my console :

Array [ [Function anonymous], [Function anonymous], ]

would you please help to find where am I making the error ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is my code :

const allPromises = async () => {
    return await Promise.all([promise1, promise2])
        .then((a) => console.log(a))
        .catch((e) => e);
};

const promise1 = useCallback(async () => {

    return userFollowers(link)
        .then((datax) => {
            dispatch({
                type: 'GET_FOLLOWING',
                payload: datax,
            });
            return Promise.resolve(datax);
        })
        .catch((e) => e);
}, [item]);

const promise2 = useCallback(async () => {
    return userFollowing(link2)
        .then((datax) => {
            dispatch({
                type: 'GET_FOLLOWERS',
                payload: datax,
            });
            return Promise.resolve(datax);
        })
        .catch((e) => e);
}, [item]);

>Solution :

promise1 and promise2 aren’t promises. They are async functions that return promises, so you need to call them

const allPromises = async () => {
    return await Promise.all([promise1(), promise2()])
        .then((a) => console.log(a))
        .catch((e) => e);
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading