---
title: "Discussion on \"Error Handling in JavaScript  (Golang Style)\" | Hashnode"
description: "Discussion on \"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.\nI 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 met..."
url: "https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Error Handling in JavaScript (Golang Style)](https://bibekkakati.hashnode.dev/error-handling-in-javascript-like-golang?utm_source=hashnode&utm_medium=feed)

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 met...

Jun 18, 2021 · 2 min read · bibekkakati.hashnode.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#go](https://hashnode.com/tag/go)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#error-handling](https://hashnode.com/tag/error-handling)

## Written by

[Bibek Kakati](https://hashnode.com/@bibekkakati)

@bibekkakati · India

  
[View profile](https://hashnode.com/@bibekkakati)

Software Engineer • Backend & AI Engineering

Software Engineer with 5+ years of experience building scalable backend and distributed systems across domains and AI.

## Responses 6

Markdown supported · ⌘↵ to post

[Zsolt Gömöri](https://hashnode.com/@zsgomori)

[Jul 1, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60dd99040970dd75de76f994)

Nice, I dig it! Thanks [Bibek Kakati](https://hashnode.com/@bibekkakati)!

I’d like to chime in with a minor yet potential improvement.

```
const getData = async () => {
  try {
    const result = await methodCall();
    return { result };
  } catch(error) {
    return { error };
  }
}

const { result, error } = getData();

if (error) {
  // …
}
```

IMO returning an object (and destructuring both `result` and `error`) is way more readable & explicit.

[Bibek Kakati](https://hashnode.com/@bibekkakati)

[Jul 1, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60dd99040970dd75de76f994#reply-60dd9bc50970dd75de76fa1e)

Yeah. We can implement like this too.

[Niranjan Borawake](https://hashnode.com/@niranjandb)

[Jun 25, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d5b055de92ef110afdbfa1)

Nice one. Reminds of [Error-first callback](https://www.geeksforgeeks.org/error-first-callback-in-node-js/) in NodeJs.

[Bibek Kakati](https://hashnode.com/@bibekkakati)

[Jun 25, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d5b055de92ef110afdbfa1#reply-60d5b1acde92ef110afdbfd8)

Thank you 😊

[Andrew Baisden](https://hashnode.com/@andrewbaisden)

[Jun 24, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d4945b5ed303057ace981b)

Interesting take on it [Bibek Kakati](https://hashnode.com/@bibekkakati) 👏

[Bibek Kakati](https://hashnode.com/@bibekkakati)

[Jun 24, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d4945b5ed303057ace981b#reply-60d495e912f77505a833c24b)

Thank you [Andrew Baisden](https://hashnode.com/@andrewbaisden)

[Adriano Luz](https://hashnode.com/@decode64)

[Jun 23, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d3a83b99fab47c98b63155)

Would love to read more about the advantages and disadvantages of the "go style" error handling.

[Bibek Kakati](https://hashnode.com/@bibekkakati)

[Jun 23, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d3a83b99fab47c98b63155#reply-60d3aeda99fab47c98b63178)

There is no major advantage and disadvantage with this pattern.

[IraManage](https://hashnode.com/@Shubham17)

[Jun 21, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d0b47ff668475d88febd05)

I like this way to handle error in JS

[Bibek Kakati](https://hashnode.com/@bibekkakati)

[Jun 21, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d0b47ff668475d88febd05#reply-60d0b5f6f668475d88febd59)

I'm glad that you liked it.

Try to use this pattern in your project.

[IraManage](https://hashnode.com/@Shubham17)

[Jun 21, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60d0b47ff668475d88febd05#reply-60d0b7b2a6a3a25dadf61653)

Yes definitely I will try to use this pattern in my project. This will make my code readable too

[Ayodele Samuel Adebayo](https://hashnode.com/@unclebigbay)

[Jun 19, 2021](https://hashnode.com/posts/error-handling-in-javascript-like-golang/60ccec45554b0d31c6053c47/comment/60cde94137952431a1712450)

🔥🔥🔥🔥
