'How to modify this React code into Async/Await?

Here is the code:

function getList() {
    return functionList()
        .then((list) => functionDetails())
        .catch((err) => console.log(err));
}

how to convert it to async/await?



Solution 1:[1]

You can wrap the call to the awaited function in a try-catch to handle the error, and then run your .then code in the finally block

async function getList() {
    try {
        const result = await functionList();
        return ((result) => functionDetails()());
    } catch(e) {
        console.log(e);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1