DEV Community


Discussion on: JavaScript: Handling errors like Go

oieduardorabelo profile image
Eduardo Rabelo Author

funny enough, in Go is a little bit harder to do the exactly same!

i recommend you to go through two blog posts:

  1. Go: Concurrency Patterns: Pipelines and cancellation
  2. Go: Learn Concurrency

you will need to use channels/goroutines to achieve the same.. as said by rhymes in this thread, you will need to use WaitGroup!

there's a merge function in the documentation link of item 1 above, where using it, you can do something like this:

func makeRequest(url string) <-chan string {
    res := make(chan string)

    go func() {
        res <- "Request started..."

        _, err := http.Get(url)

        if err != nil {
            res <- "Failed."

        res <- "Done!"

    return res

func main() {
    c1 := makeRequest("")
    c2 := makeRequest("")

    for n := range merge(c1, c2) {

running it will print:

$ go run main.go 
Request started...
Request started...

i've been using it since I discovered it :)

iaziz786 profile image
Mohammad Aziz

This looks simple and powerful. Probably I'll have to dig deeper to understand it. Thanks for the resources.

theodesp profile image
Theofanis Despoudis

I think you can also use errgroup to handle errors gracefully