ASYNC & AWAIT IN JAVASCRIPT PART THREE
HANDLING ERRORS If a promise results in an error, you handle it with a catch call, like this: const getOne = async (success = true) => { if (success) return 1 throw new Error('Failure!') } getOne(false) .catch(error => console.log(error)) // Failure!
If you want to handle an error in an asynchronous function, you need to use a try/catch call. const test = async _ => { try { const one = await getOne(false) } catch (error) { console.log(error) // Failure! } } test()

Async await in JavaScript - part-3

  • 1.
  • 2.
    HANDLING ERRORS If apromise results in an error, you handle it with a catch call, like this: const getOne = async (success = true) => { if (success) return 1 throw new Error('Failure!') } getOne(false) .catch(error => console.log(error)) // Failure!
  • 3.
    If you wantto handle an error in an asynchronous function, you need to use a try/catch call. const test = async _ => { try { const one = await getOne(false) } catch (error) { console.log(error) // Failure! } } test()