Error Handling in JavaScript (Golang Style)

In this short article, we are going to see how we can handle error in JavaScript in Golang style.

I am assuming, you have some experience with JavaScript and you are aware of the issues with error handling like throwing an exception to the parent meth…


This content originally appeared on DEV Community and was authored by Bibek

In this short article, we are going to see how we can handle error in JavaScript in Golang style.

I am assuming, you have some experience with JavaScript and you are aware of the issues with error handling like throwing an exception to the parent method from try-catch block or chaining multiple then-blocks and implementing logic inside it. These things can easily mess up with the code making it difficult to read.

Golang avoids these type of problems by handling errors/exceptions atomically.

Error handling in Golang

result, err := methodCall()
if err != nil {
  // handle error
}
// do something with the result

We can use a similar pattern in JavaScript with the help of a try-catch block like this.

const getData = async () => {
  try {
    const result = await methodCall();
    return [result, null];
  } catch(error) {
    return [null, error];
  }
}

If any error occurs, we are returning the error in the second position of the array and the result as null in the first position.

If there is no error, we are returning the result in the first position and error as null in the second position.

Now we can call the getData method then handle the result and error like this.

const [result, error] = await getData();
if (error !== null) {
  // handle the error
}
// do something with the result

This pattern of error handling makes it very easy to read and understand the code.

Let me know what do you think about this pattern.

Originally published on blog.bibekkakati.me

Thank you for reading ?

If you enjoyed this article or found it helpful, give it a thumbs-up ?

Feel free to connect ?

Twitter | Instagram | LinkedIn

If you like my work and want to support it, you can do it here. I will really appreciate it.




This content originally appeared on DEV Community and was authored by Bibek


Print Share Comment Cite Upload Translate Updates
APA

Bibek | Sciencx (2021-06-18T19:00:16+00:00) Error Handling in JavaScript (Golang Style). Retrieved from https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/

MLA
" » Error Handling in JavaScript (Golang Style)." Bibek | Sciencx - Friday June 18, 2021, https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/
HARVARD
Bibek | Sciencx Friday June 18, 2021 » Error Handling in JavaScript (Golang Style)., viewed ,<https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/>
VANCOUVER
Bibek | Sciencx - » Error Handling in JavaScript (Golang Style). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/
CHICAGO
" » Error Handling in JavaScript (Golang Style)." Bibek | Sciencx - Accessed . https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/
IEEE
" » Error Handling in JavaScript (Golang Style)." Bibek | Sciencx [Online]. Available: https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/. [Accessed: ]
rf:citation
» Error Handling in JavaScript (Golang Style) | Bibek | Sciencx | https://www.scien.cx/2021/06/18/error-handling-in-javascript-golang-style/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.