Skip to content

Promise.thenMap

Richard Hightower edited this page Jun 8, 2016 · 2 revisions

The method Promise.thenMap converts one type of promise into another.

Let's say for example you are storing data into Cassandra. You want to convert a promise that returns a Cassandra ResultSet into a promise that returns a Boolean.

Example using converting a promise for Cassandra ResultSet into a boolean promise.

        final Promise<ResultSet> resultSetPromise = Promises.<ResultSet>promise();
        final Promise<Boolean> promise = resultSetPromise
                .thenMap(ResultSet::wasApplied) // <-------------- see thenMap
                .catchError((error) -> {
                        if (error instanceof DriverException) {
                            logger.error("Error " + message, error);
                            errorCount.incrementAndGet();
                        }
                });
        /* registerCallback is from Reakt Guava Bridge 
           It converts ListableFutures into Reakt promises (to/fro).
        */
        registerCallback(resultSetFuture, resultSetPromise); 

Here is a simpler example that turns an Employee Promise into a Sheep Promise.

Converting an employee into a sheep.
        Promise<Employee> employeePromise = Promises.<Employee>promise();

        Promise<Sheep> sheepPromise = employeePromise
                .thenMap(employee1 -> new Sheep(employee1.id));


        employeeService.lookupEmployee("id123", employeePromise);
You can’t perform that action at this time.