Skip to content

gophernment/egg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

egg

CI

A tiny lazy future/promise for Go. New starts a Worker in its own goroutine right away and returns a Responder you can call later to block for the result.

Install

go get github.com/gophernment/egg

Usage

package main

import (
	"errors"
	"fmt"

	"github.com/gophernment/egg"
)

func main() {
	r := egg.New(func() any {
		return "hello"
	})

	fmt.Println(<-r()) // "hello"

	// If the Worker panics, the channel receives an error instead of
	// crashing the program.
	r = egg.New(func() any {
		panic(errors.New("boom"))
	})

	if err, ok := (<-r()).(error); ok {
		fmt.Println("worker panicked:", err)
	}
}

Behavior

  • The result channel is buffered, so the goroutine never blocks trying to send even if you never call the Responder.
  • A panic inside the Worker is recovered and delivered on the same channel as an error — check with a type assertion to tell it apart from a normal result.

License

MIT — see LICENSE.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages