There's an article called "From Events to Futures and Promises and Back" by Martin Sulzmann (published in February 2016) that talks about what you are trying to achieve. The abstract says:
Channel-based events and futures / promises are powerful, but seemingly different, concepts of parallel programming. We show that one concept can be expressed in terms of another with surprisingly little effort. Our results offer lightweight library approaches for implementing events and futures / promises. Empirical results show that our approach works well in practice.
According to the document, futures look like this:
type Comp struct { value interface{} ok bool } type Future chan Comp func future(f func() (interface{}, bool)) Future { future := make(chan Comp) go func() { v, o := f() c := Comp{v, o} for { future <- c } }() return future }
While Promises are implemented as follows:
type Promise struct { lock chan int ft Future full bool } func promise() Promise { return Promise{make(chan int, 1), make(chan Comp), false} } func (pr Promise) future() Future { return pr.ft }
Read paper for parts, combinators, etc.
source share