T
- the type of promised valuepublic interface PromiseOperations<T>
These methods are available on Promise
and SuccessPromise
, but are defined on this separate interface for clarity.
Modifier and Type | Method and Description |
---|---|
<O> Promise<O> |
blockingMap(Function<? super T,? extends O> transformer)
Like
map(Function) , but performs the transformation on a blocking thread. |
Promise<T> |
cache() |
<O> Promise<O> |
flatMap(Function<? super T,? extends Promise<O>> transformer)
Transforms the promised value by applying the given function to it that returns a promise for the transformed value.
|
<O> Promise<O> |
map(Function<? super T,? extends O> transformer)
Transforms the promised value by applying the given function to it.
|
Promise<T> |
onNull(NoArgAction action) |
Promise<T> |
route(Predicate<? super T> predicate,
Action<? super T> action) |
<O> Promise<O> map(Function<? super T,? extends O> transformer)
import ratpack.exec.Execution;
import ratpack.exec.Promise;
import ratpack.func.Function;
import ratpack.test.UnitTest;
import ratpack.test.exec.ExecHarness;
import ratpack.test.exec.ExecResult;
import java.util.concurrent.Callable;
public class Example {
public static void main(String[] args) throws Exception {
try (ExecHarness harness = UnitTest.execHarness()) {
ExecResult<String> result = harness.execute(new Function<Execution, Promise<String>>() {
public Promise<String> apply(Execution execution) throws Exception {
return execution.getControl()
.blocking(new Callable<String>() {
public String call() throws Exception {
return "foo";
}
})
.map(new Function<String, String>() {
public String apply(String s) throws Exception {
return s.toUpperCase();
}
})
.map(new Function<String, String>() {
public String apply(String s) throws Exception {
return s + "-BAR";
}
});
}
});
assert result.getValue().equals("FOO-BAR");
}
}
}
O
- the type of the transformed objecttransformer
- the transformation to apply to the promised value<O> Promise<O> blockingMap(Function<? super T,? extends O> transformer)
map(Function)
, but performs the transformation on a blocking thread.
This is simply a more convenient form of using ExecControl.blocking(java.util.concurrent.Callable)
and flatMap(Function)
.
O
- the type of the transformed objecttransformer
- the transformation to apply to the promised value, on a blocking thread<O> Promise<O> flatMap(Function<? super T,? extends Promise<O>> transformer)
This is useful when the transformation involves an asynchronous operation.
import ratpack.exec.ExecControl;
import ratpack.exec.Execution;
import ratpack.exec.Promise;
import ratpack.func.Function;
import ratpack.test.UnitTest;
import ratpack.test.exec.ExecHarness;
import ratpack.test.exec.ExecResult;
import java.util.concurrent.Callable;
public class Example {
public static void main(String[] args) throws Exception {
try (ExecHarness harness = UnitTest.execHarness()) {
ExecResult<String> result = harness.execute(new Function<Execution, Promise<String>>() {
public Promise<String> apply(Execution execution) throws Exception {
final ExecControl control = execution.getControl();
return control
.blocking(new Callable<String>() {
public String call() throws Exception {
return "foo";
}
})
.flatMap(new Function<String, Promise<String>>() {
public Promise<String> apply(final String s) throws Exception {
return control.blocking(new Callable<String>() {
public String call() throws Exception {
return s.toUpperCase();
}
});
}
})
.map(new Function<String, String>() {
public String apply(String s) throws Exception {
return s + "-BAR";
}
});
}
});
assert result.getValue().equals("FOO-BAR");
}
}
}
In the above example, flatMap()
is being used because the transformation requires a blocking operation (it doesn't really in this case, but that's what the example is showing).
In this case, it would be more convenient to use blockingMap(Function)
.
O
- the type of the transformed objecttransformer
- the transformation to apply to the promised valueblockingMap(Function)
Promise<T> onNull(NoArgAction action)