Home > Net >  why my promise all function returns anonymous function?
why my promise all function returns anonymous function?

Time:11-06

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 ?

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]);

CodePudding user response:

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);
};
  • Related