Skip to content

Instantly share code, notes, and snippets.

@bnm3k
Last active July 3, 2020 12:20
Show Gist options
  • Save bnm3k/2819f296cacc7270d8c0cc75c82f5ee1 to your computer and use it in GitHub Desktop.
Save bnm3k/2819f296cacc7270d8c0cc75c82f5ee1 to your computer and use it in GitHub Desktop.
JS-style promises in Golang
package promises
import (
"fmt"
"time"
)
/*
Credits to github.com/Workiva/go-datastructures/blob/master/futures/futures.go
whose interface/API and tests for Futures I generously borrowed, but I restricted my version
to using channels for mutual exclusion and waiting.
Go race detector did not detect any data races for my version
Performance between my version and workiva's version is almost the same
goos: darwin
goarch: amd64
pkg: ...
BenchmarkFutureWorkiva-4 413845 2811 ns/op
BenchmarkMyVersion-4 341008 2947 ns/op
PASS
ok ... 3.200s
*/
type Promise interface{
GetResult() (interface{}, error)
HasResult() bool
}
type promise struct {
item interface{}
err error
completed chan struct{}
}
// GetResult waits until results is available or timeout occurs
func (p *promise) GetResult() (interface{}, error) {
<-p.completed
return p.item, p.err
}
// HasResult returns true if promise completed
func (p *promise) HasResult() bool {
select {
case <-p.completed:
return true
default:
return false
}
}
func (p *promise)await(ch <-chan interface{}, timeout time.Duration, sigCh chan<- struct{}) {
close(sigCh)
t := time.NewTimer(timeout)
select {
case item := <-ch:
p.item = item
t.Stop()
case <-t.C:
p.err = fmt.Errorf(`timeout after %f seconds`, timeout.Seconds())
}
close(p.completed)
}
func NewPromise(result <-chan interface{}, timeout time.Duration) Promise {
p := &promise{
completed: make(chan struct{}),
}
sigCh := make(chan struct{})
go p.await(result, timeout, sigCh)
<-sigCh
return p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment