Write elegant asynchronous code using promises.
p := promise.Create(func() (interface{}, interface{}) {
time.Sleep(100 * time.Milliseconds)
return 42, nil
})
fmt.Println("Waiting...")
x,_ := p.Wait()
fmt.Println("x =", x)
prints
Waiting...
x = 42
x,err := promise.Create(func() (interface{}, interface{}) {
return 42, nil
}).Then(func(res interface{}) (interface{}, interface{}) {
x := res.(int)
return x * 2, nil
}).Wait()
x,err := promise.Create(func() (interface{}, interface{}) {
return promise.Create(func() (interface{}, interface{}) { return 42, nil }, nil
}).Then(func(res interface{}) (interface{}, interface{}) {
x := res.(int)
return x * 2
}).Wait()
x,err := promise.Create(func() (interface{}, interface{}) {
return [...]*SPromise{
Create(func() (interface{}, interface{}) { return 1, nil }),
Create(func() (interface{}, interface{}) { return 2, nil }),
Create(func() (interface{}, interface{}) { return 3, nil }),
}, nil
}).All().Wait()
Uses a single GoRoutine per promise to run serial links in the chain and uses metuxes instead of channels to synchronize the promises.
- Each: Run a function on all items of an array and return an array
- Map: Run a function on each item of a map and return a map, order not guarenteed
- Reduce: Run a function to reduce an array to a single value, order not guarenteed
- Filter: Run a function to filter an array to contain only specific values
- Documentation
- License