0
votes

My code doesn't work, it always returning

Invariant Violation: Objects are not valid as a React child (found: object with keys {_40, _65, _55, _72}). If you meant to render a collection of children, use an array instead.

is there any wrong syntax or logic in there? here is my code:

const asyncTest1= async() => {
    try {
        noteAction({ type: SET_LOADING, payload: true });
        const response = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('Async Test Load');
            }, 3000);
        });
        const adding = noteAction({ type: ADD_NOTE, payload: response });
        const setLoadingFalse = noteAction({ type: SET_LOADING, payload: false });
        const result = await Promise.all([response, adding, setLoadingFalse]);
        return result;
    } catch (e) {
        console.log(e);
    }
};

but with no async/await version, my code is working:

  const asyncTest2= () => {
        try {
            noteAction({ type: SET_LOADING, payload: true });
            const result = new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve('Async Test Load');
                }, 3000);
            });
            return result
            .then(response => noteAction({ type: ADD_NOTE, payload: response }))
            .then(response => noteAction({ type: SET_LOADING, payload: false }));
        } catch (e) {
            console.log(e);
        }
    };
1
I doubt the error comes from this piece of code. Please add the stack trace, and post the relevant parts of your code - probably noteAction and the render method of the affected component? - Bergi
And no, this has nothing to do with promises or asynchrony per se. - Bergi
Did you have a look at Invariant Violation: Objects are not valid as a React child and related ones? - Bergi
when i use the promise .then version. it works, the error happen when i use async/await instead of the old promise version. @Bergi - ukiyakimura
Which ".then version" are you talking about? Please edit your code to include the working code so that we can compare. - Bergi

1 Answers

0
votes

but with no async/await version, my code is working

The equivalent of that with async/await syntax would not use any Promise.all, it would be

const asyncTest2 = () => {
    try {
        noteAction({ type: SET_LOADING, payload: true });
    } catch (e) {
        console.log(e);
    }
    var result = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Async Test Load');
        }, 3000);
    });
    var response = await result;
    var response = await noteAction({ type: ADD_NOTE, payload: response }));
    return noteAction({ type: SET_LOADING, payload: false }));
};

I can't tell whether this is what you actually want, but at least it would work the same as the then version.