I dislike how the Java Promises and the Scala code unnecessarily hide the reality of the computation from the developer, take a Go version as counterpoint:
func stockForProdsTO(uid int, timeout time.Duration) (*Stock, error) {
stockCh := make(chan *Stock, 1)
go func() {
orders := ordersForUser(userById(uid).email)
stockCh <- stockForProds(prodsForOrders(orders))
}()
select {
case result := <-stockCh:
return result, nil
case <-time.After(timeout):
return nil, errors.New("Could not get stock for product before timeout.")
}
}
Comments
I dislike how the Java Promises and the Scala code unnecessarily hide the reality of the computation from the developer, take a Go version as counterpoint: