Package org.forgerock.util.promise

An implementation of the Promise API in Java. Promises provide a simple API for chaining together asynchronous tasks and result transformation.

Example using CREST:

 final ConnectionFactory server = getConnectionFactory();
 final AtomicReference<Connection> connectionHolder = new AtomicReference<Connection>();
 final Promise<Resource, ResourceException> promise = server.getConnectionAsync()
     .thenAsync(new AsyncFunction<Connection, Resource, ResourceException>() {
         // Read resource.
         public Promise<Resource, ResourceException> apply(final Connection connection)
                 throws ResourceException {
             connectionHolder.set(connection); // Save connection for later.
             return connection.readAsync(ctx(), Requests.newReadRequest("users/1"));
         }
     }).thenAsync(new AsyncFunction<Resource, Resource, ResourceException>() {
         // Update resource.
         public Promise<Resource, ResourceException> apply(final Resource user) throws ResourceException {
             return connectionHolder.get().updateAsync(ctx(),
                     Requests.newUpdateRequest("users/1", userAliceWithIdAndRev(1, 1)));
         }
     }).then(new SuccessHandler<Resource>() {
         // Check updated resource.
         public void handleResult(final Resource user) {
             // Update successful!
         }
     }).thenAlways(new Runnable() {
         // Close the connection.
         public void run() {
             final Connection connection = connectionHolder.get();
             if (connection != null) {
                 connection.close();
             }
         }
     });
 
See Also:
Promises/A+