DEV Community

Discussion on: JavaScript: Handling errors like Go

Collapse
 
rhymes profile image
rhymes

You probably need to use a WaitGroup.

This is the example you can find in the documentation link:

package main

import (
    "sync"
)

type httpPkg struct{}

func (httpPkg) Get(url string) {}

var http httpPkg

func main() {
    var wg sync.WaitGroup
    var urls = []string{
        "http://www.golang.org/",
        "http://www.google.com/",
        "http://www.somestupidname.com/",
    }
    for _, url := range urls {
        // Increment the WaitGroup counter.
        wg.Add(1)
        // Launch a goroutine to fetch the URL.
        go func(url string) {
            // Decrement the counter when the goroutine completes.
            defer wg.Done()
            // Fetch the URL.
            http.Get(url)
        }(url)
    }
    // Wait for all HTTP fetches to complete.
    wg.Wait()
}
Collapse
 
foresthoffman profile image
Forest Hoffman

WaitGroup is awesome 🎉

Collapse
 
iaziz786 profile image
Mohammad Aziz

I was not aware of WaitGroup. Thanks!