当前位置: 首页>>代码示例>>Java>>正文


Java Promise.async方法代码示例

本文整理汇总了Java中ratpack.exec.Promise.async方法的典型用法代码示例。如果您正苦于以下问题:Java Promise.async方法的具体用法?Java Promise.async怎么用?Java Promise.async使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ratpack.exec.Promise的用法示例。


在下文中一共展示了Promise.async方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: promise

import ratpack.exec.Promise; //导入方法依赖的package包/类
/**
 * Converts an {@link Observable} into a {@link Promise}, for all of the observable's items.
 * <p>
 * This method can be used to simply adapt an observable to a promise, but can also be used to bind an observable to the current execution.
 * It is sometimes more convenient to use {@link #promise(ObservableOnSubscribe)} over this method.
 * <p>
 * <pre class="java">{@code
 * import ratpack.rx.RxRatpack;
 * import ratpack.test.exec.ExecHarness;
 * import rx.Observable;
 * import java.util.List;
 * import java.util.Arrays;
 * import public static org.junit.Assert.assertEquals;
 * <p>
 * public class Example {
 * public static class AsyncService {
 * public <T> Observable<T> observe(final T value) {
 * return Observable.create(subscriber ->
 * new Thread(() -> {
 * subscriber.onNext(value);
 * subscriber.onCompleted();
 * }).start()
 * );
 * }
 * }
 * <p>
 * public static void main(String[] args) throws Throwable {
 * List<String> results = ExecHarness.yieldSingle(execution ->
 * RxRatpack.promise(new AsyncService().observe("foo"))
 * ).getValue();
 * <p>
 * assertEquals(Arrays.asList("foo"), results);
 * }
 * }
 * }</pre>
 * <p>
 * <p>
 * This method uses {@link Observable#toList()} to collect the observable's contents into a list.
 * It therefore should not be used with observables with many or infinite items.
 * <p>
 * If it is expected that the observable only emits one element, it is typically more convenient to use {@link #promiseSingle(Observable)}.
 * <p>
 * If the observable emits an error, the returned promise will fail with that error.
 * <p>
 * This method must be called during an execution.
 *
 * @param observable the observable
 * @param <T>        the type of the value observed
 * @return a promise that returns all values from the observable
 * @throws UnmanagedThreadException if called outside of an execution
 * @see #promiseSingle(Observable)
 */
public static <T> Promise<List<T>> promise(Observable<T> observable) throws UnmanagedThreadException {
  return Promise.async(f -> observable.toList().subscribe(f::success, f::error));
}
 
开发者ID:drmaas,项目名称:ratpack-rx2,代码行数:56,代码来源:RxRatpack.java

示例2: promiseSingle

import ratpack.exec.Promise; //导入方法依赖的package包/类
/**
 * Converts an {@link Observable} into a {@link Promise}, for the observable's single item.
 * <p>
 * This method can be used to simply adapt an observable to a promise, but can also be used to bind an observable to the current execution.
 * It is sometimes more convenient to use {@link #promiseSingle(ObservableOnSubscribe)} over this method.
 * <p>
 * <pre class="java">{@code
 * import ratpack.rx.RxRatpack;
 * import ratpack.test.exec.ExecHarness;
 * import rx.Observable;
 * import public static org.junit.Assert.assertEquals;
 * <p>
 * public class Example {
 * public static class AsyncService {
 * public <T> Observable<T> observe(final T value) {
 * return Observable.create(subscriber ->
 * new Thread(() -> {
 * subscriber.onNext(value);
 * subscriber.onCompleted();
 * }).start()
 * );
 * }
 * }
 * <p>
 * public static void main(String[] args) throws Throwable {
 * String result = ExecHarness.yieldSingle(execution ->
 * RxRatpack.promiseSingle(new AsyncService().observe("foo"))
 * ).getValue();
 * <p>
 * assertEquals("foo", result);
 * }
 * }
 * }</pre>
 * <p>
 * <p>
 * This method uses {@link Observable#singleElement()} to enforce that the observable only emits one item.
 * If the observable may be empty, then use {@link Observable#single(Object)} to provide a default value.
 * <p>
 * <pre class="java">{@code
 * import ratpack.rx.RxRatpack;
 * import ratpack.test.exec.ExecHarness;
 * import rx.Observable;
 * import public static org.junit.Assert.assertEquals;
 * <p>
 * public class Example {
 * public static void main(String[] args) throws Throwable {
 * String result = ExecHarness.yieldSingle(execution ->
 * RxRatpack.promiseSingle(Observable.<String>empty().singleOrDefault("foo"))
 * ).getValue();
 * assertEquals("foo", result);
 * }
 * }
 * }</pre>
 * <p>
 * <p>
 * If it is expected that the observable may emit more than one element, use {@link #promise(Observable)}.
 * <p>
 * If the observable emits an error, the returned promise will fail with that error.
 * If the observable emits no items, the returned promise will fail with a {@link java.util.NoSuchElementException}.
 * If the observable emits more than one item, the returned promise will fail with an {@link IllegalStateException}.
 * <p>
 * This method must be called during an execution.
 *
 * @param observable the observable
 * @param <T>        the type of the value observed
 * @return a promise that returns the sole value from the observable
 * @see #promise(Observable)
 * @see #promiseSingle(ObservableOnSubscribe)
 */
public static <T> Promise<T> promiseSingle(Observable<T> observable) throws UnmanagedThreadException {
  return Promise.async(f -> observable.singleElement().subscribe(f::success, f::error));
}
 
开发者ID:drmaas,项目名称:ratpack-rx2,代码行数:73,代码来源:RxRatpack.java


注:本文中的ratpack.exec.Promise.async方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。