...
Guava provides a similar capability with sameThreadExecutor. It is somewhat related to the JDK's CallerRunsPolicy for rejected execution.
Further Analysis
Delivery
The APIs in this proposal can be delivered either as a library or as an extension to the Clojure language.
Extending the Clojure language has the benefits of enhancing the existing promise and future in a standard way. Having a standard mechanism for callbacks at the language level will facilitate sharing callbacks across different libraries and frameworks.
Exceptions
A callback function (really, any function) can do one of three things:
...
Promises do not provide an API to report partial progress during a long-running operation. Progress reporting can be handled through other means such as watches.
ClojureScript
Promises can be implemented in ClojureScript, subject to the following limitations:
- There is no notion of executors Callbacks execute either synchronously or asynchronously apart from "execute immediately" or "execute asynchronously" (e.g., with with
setTimeout) derefon an unrealized promise cannot block; instead it throws an exception
Discussions
- Promise/Futures with callbacks (Clojure-dev group)
- lamina and channels-driven concurrency with clojure (Clojure group)
- reactive programing (dev.clojure.org)
- can a better Future be exposed by API? (Datomic group)
...
References / Related Work
- Java
- Listenable Futures in Google Guava
- ListenableFuture in Async HTTP Client
- Scala
- JavaScript
- CommonJS Promises/A
- Promises/A+ (extension of Promises/A)
- You're Missing the Point of Promises
- Q
- Angular JS promise/deferred
- Scala
- Java
- Listenable Futures in Google Guava
- ListenableFuture in Async HTTP Client
- C# / .NET
- Python
Comparison to Guava ListenableFuture
Guava's ListenableFuture takes the same basic approach of passing a Runnable and an Executor for the callback. It should be trivial to extend the INotify and IDeliver protocols to Guava's ListenableFuture and SettableFuture.
The implementation in Guava is more sophisticated than the one in cljque, using an AbstractQueuedSynchronizer instead of monitors.