0

Here is the code:

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

how to convert it to async/await?

7

1 Answer 1

0

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);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Nope, this has a different semantics. It calls functionDetails in any case, whereas OP calls it only when functionList didn't reject ...
Will the return inside the catch not prevent the finally block from running if there's an error?
No, the finally is executed no matter what you do in the try or the catch block. That's the whole purpose of finally
So if we raise the call to functionDetails inside the try block that seems to work correctly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.