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

Will try-catch catch an error if the function inside of it doesn't have an await in front of it?

Imagine I have the following function:

const test = async () => {
    const response = await axios.get('testurl');
    return response;
}

And then I try to use the function like this:

try {
    test();
} catch (error) {
    // handle errors
}

Will the try-catch block actually catch the error if I call the test() function without an await in front of it in the try-catch block?

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

>Solution :

No. Without an await the calling function will not go to sleep and wait for the promise to resolve or error. It will continue running to its own completion.

Later, when the promise errors, the error will be unhandled.

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