Futures

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 1 Imported by: 0

README

go-async

Documentation Go Report Card

A dead simple type safe implementation of the async - await pattern in golang

But why?

because:
  1. it can be done
  2. it cut's down on channel boilerplate
  3. it gives programmers coming from languages like Rust, TypeScript and C# a familiar interface for concurrent programming

How to install:

  • go get https://github.com/F-bh/go-async
  • it requires at least golang version 1.18 to use

What is included

  • 1 type called "Future"
  • 1 function to turn a given function into a "Future"
    • Async()
  • 2 methods to await the result of a given "Future"
    • Await()
    • AwaitWithTimeout()

Examples

package examples

import (
	"github.com/F-bh/go-async"
	"time"
)

func example(){
	future := Futures.Async(func() string{
		time.Sleep(500)
		return "Hello World"
	})

	println(future.Await())

	futureTimeout := Futures.Async(func() string{
		time.Sleep(50000000)
		return "unused"
	})

	val, ok := futureTimeout.AwaitWithTimeout(500)
	if !ok{
		println("timed out")
	}else{
		println(*val)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Future

type Future[T any] struct {
	// contains filtered or unexported fields
}

func Async

func Async[T any](f func() T) Future[T]

Async a simple way to turn a function into a Future if the function you want to execute takes any parameters consider wrapping it in an anonymous function

func (Future[T]) Await

func (f Future[T]) Await() T

Await blocks until the Future is resolved returns the Future's underlying value may block indefinitely

func (Future[T]) AwaitWithTimeout

func (f Future[T]) AwaitWithTimeout(timeout time.Duration) (*T, bool)

AwaitWithTimeout blocks until the Future is resolved or the timeout was reached returns either a pointer to the Future's underlying (*value, true) or (nil, false) if the timeout was reached a timeout does NOT stop the Future's underlying go routine

Jump to

Keyboard shortcuts

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