Name |
Deprecated : see ceylon.promises |
---|---|
Backends | JVM |
Maven coordinates | |
Compatible Ceylon release |
JVM: 1.0.0 (outdated) |
Published | Nov 13, 2013 |
Stats |
Downloads (JVM): 642 Source downloads: 720 |
Authors |
Julien Viet |
License | ASL2 |
Description |
A module that provides Promises/A+ semantics adapted to the Ceylon language. This implementation conforms to the Promises/A+ specification although it is not a formal implementation due to the adaptation to the Ceylon language and type system. However it attempts to respect the meaning of the specification and the same terminology. The modules provides:
GoalIf a function cannot return a value or throw an exception without blocking, it can return a promise instead. A promise is an object that represents the return value or the thrown exception that the function may eventually provide. A promise can also be used as a proxy for a remote object to overcome latency. UsagePromiseThe [[Promise]] interface expose the Promise<String> promise = getPromise(); promise.then_( (String s) => print("The promise is resolved with " + s), (Exception e) => print("The promise is rejected with " + e.message)); The first function is called the DeferredA [[Deferred]] object provides an implementation of the [[Promise]] interface and can be transitionned to a fulfillment or a reject resolution. It should remain private to the part of the code using it and only its promise should be visible. The [[Promise]] of a deferred can be retrieved with its value deferred = Deferred<String>(); return deferred.promise; The [[Deferred]] object implements the [[Transitionnable]] interface which provides two methods for resolving the promise:
For example: value deferred = Deferred<String>(); void doOperation() { try { String val = getValue(); deferred.resolve(val); } catch(Exception e) { deferred.reject(e); } } Chaining promisesWhen invoking the Promise<Integer> promiseOfInteger = getPromiseOfInteger(); Promise<String> promiseOfString = promiseOfInteger.then_((Integer i) => i.string); promiseOfString.then_((String s) => print("Completed with " + s)); or shorter getPromiseOfInteger().then_((Integer i) => i.string).then_((String s) => print("Completed with " + s)); Combining promisesPromises can be combined into a single promise that is resolved when all the combined promises are resolved. If one of the promise is rejected then the combined promise is rejected. Promise<String> promiseOfInteger = getPromiseOfString(); Promise<Integer> promiseOfString = getPromiseOfInteger(); promiseOfInteger.and(promiseOfString).then_( (Integer i, String s) => print("All resolved"), (Exception e) => print("One failed")); There are two things to note here:
AlwaysThe promise.always((String|Integer) p => print("done!"); Always is useful for implementing a finally clause in a chain of promise. Feeding with a promiseDeferred can be transitionned with promises instead of a value: Deferred<String> deferred1 = getDeferred1(); Deferred<String> deferred2 = getDeferred2(); deferred1.resolve(deferred2); Similarly the callback can return a promise instead of a value: Deferred<String> deferred = Deferred<String>(); promise.then__((String s) => deferred.promise); FutureSometimes it is convenient to block until a promise is resolved, for this purpose a promise can be turned into a [[Future]] via its future: Promise<String> promise = getPromise(); Future<String|Exception> future = promise.future(); String|Exception resolution = future.get(10000); Keep in mind that this is not the way you should use promises as this defeats the non blocking model. Nevertheless can be useful to block (for instance: unit testing purposes). Thread safetyThe implementation is thread safe and use non blocking algorithm for maintaining the state of a deferred object. Differences with the original specification:
|
Dependencies |
java.base/7 JDK
|
Usage |
import vietj.promises "0.5.0"; $ ceylon run vietj.promises/0.5.0 <property name="ceylon.home" value="…"/> <property name="ceylon.ant.lib" value="${ceylon.home}/lib/ceylon-ant.jar"/> <path id="ant-tasks"> <pathelement path="${ceylon.ant.lib}"/> </path> <typedef resource="com/redhat/ceylon/ant/antlib.xml" classpathref="ant-tasks"/> <target name="run"> <ceylon-run module="vietj.promises/0.5.0"/> </target> |
Module links |
Members Imported By Home Code repository Issue tracker Browse Download .car No .js archive Download source archive Download module documentation View API documentation |