gaw

package module
v0.0.0-...-b46ffc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 2 Imported by: 0

README

Gaw (Go Async Await)

Small library to mimic Node's Async Await with Golang

gaw CI Go Reference

Requirements

  • Go 1.18+ (because gaw uses generic features)

Install

$ go get github.com/telkomdev/gaw

Basic Usage

package main

import (
	"context"
	"fmt"
	"github.com/telkomdev/gaw"
	"time"
)

type Event struct {
	ID   string
	Name string
}

func (e *Event) String() string {
	return fmt.Sprintf("event id: %s\nevent name: %s\n", e.ID, e.Name)
}

func main() {
	async := gaw.Async[*Event](context.Background(), func() (*Event, error) {
		// simulate heavy work that takes 3 seconds to finish
		time.Sleep(time.Second * 3)

		return &Event{ID: "111", Name: "Order Request"}, nil
	})

	// do other work while waiting async to finish
	fmt.Println("do other work")

	// call Await
	async.Await()

	fmt.Println("work done")

	// check if its error
	if async.IsErr() {
		fmt.Printf("async error: %v\n", async.Err())
	} else {
        // no error
        // get the value
		fmt.Println(async.Get())
	}
}

AsyncAll

Call multiple function in async mode, and get the multiple results

package main

import (
	"context"
	"fmt"
	"net"
	"net/http"
	"time"

	"github.com/telkomdev/gaw"
)

func httpGet(url string) (*http.Response, error) {
	transport := &http.Transport{
		Dial: (&net.Dialer{
			Timeout: 5 * time.Second,
		}).Dial,
		TLSHandshakeTimeout: 5 * time.Second,
		IdleConnTimeout:     10 * time.Second,
	}

	httpClient := &http.Client{
		//Timeout:   time.Second * 10,
		Transport: transport,
	}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	response, err := httpClient.Do(req)
	if err != nil {
		return nil, err
	}

	return response, nil
}

func main() {
	url := "https://www.w3schools.com/js/default.asp"

	f1 := func() (*http.Response, error) {
		resp, err := httpGet(url)

		return resp, err
	}

	f2 := func() (*http.Response, error) {
		resp, err := httpGet(url)

		return resp, err
	}

	f3 := func() (*http.Response, error) {
		resp, err := httpGet(url)

		return resp, err
	}

	asyncAll := gaw.AsyncAll[*http.Response](ctx, f1, f2, f3)

	// do other work while waiting async to finish
	fmt.Println("do other work")

	// call Await
	asyncAll.Await()

	fmt.Println("work done")

	for _, resp := range asyncAll.Get() {

		fmt.Println(resp)
		fmt.Println("----")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Function

type Function[R any] func() (R, error)

Function base function type of gaw

type Result

type Result[R any] struct {
	// contains filtered or unexported fields
}

Result represent things that eventually available

func Async

func Async[R any](ctx context.Context, function Function[R]) *Result[R]

Async will invoke Function in async mode

func NewResult

func NewResult[R any]() *Result[R]

NewResult the Result constructor

func (*Result[R]) Await

func (f *Result[R]) Await()

Await will determine wheter Result is done or not done

func (*Result[R]) Err

func (f *Result[R]) Err() error

Err will return error

func (*Result[R]) Get

func (f *Result[R]) Get() R

Get will return Result value

func (*Result[R]) IsErr

func (r *Result[R]) IsErr() bool

IsErr will return true if err not nil

type Results

type Results[R any] []*Result[R]

Results represent multiple things that eventually available

func AsyncAll

func AsyncAll[R any](ctx context.Context, functions ...Function[R]) Results[R]

AsyncAll will invoke multiple Function in async mode

func (Results[R]) Await

func (rs Results[R]) Await()

Await will determine wheter all Results is done or not done

func (Results[R]) Get

func (rs Results[R]) Get() []R

Get will return Results multiple value

Directories

Path Synopsis
_examples
asyncerror command
basic command
fireforget command
httprequest command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL