本文整理了Java中com.englishtown.promises.Promise
类的一些代码示例,展示了Promise
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Promise
类的具体详情如下:
包路径:com.englishtown.promises.Promise
类名称:Promise
[英]Promises/A+ implementation
[中]承诺/A+实施
代码示例来源:origin: ef-labs/vertx-jersey
/**
* {@inheritDoc}
*/
@Override
public void start(Future<Void> startedResult) throws Exception {
jerseyServer.createServer()
.then(server -> {
startedResult.complete();
return null;
})
.otherwise(t -> {
startedResult.fail(t);
return null;
});
}
}
代码示例来源:origin: ef-labs/when.java
/**
* Handle the ultimate fulfillment value or rejection reason, and assume
* responsibility for all errors. If an error propagates out of result
* or handleFatalError, it will be rethrown to the host, resulting in a
* loud stack track on most platforms and a crash on some.
*
* @param onResult function called when fulfilled
* @param <U> onResult thenable return type
*/
default <U> void done(Function<T, ? extends Thenable<U>> onResult) {
done(onResult, null);
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_inspect_when_inspecting_promises_should_return_rejected_state_for_rejected_promise() {
Promise<Object> promise = when.reject(sentinelEx);
promise.then(fail.onFulfilled, (t) -> {
assertRejected(promise.inspect(), sentinelEx);
return null;
}).ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testElse_should_resolve_normally_if_previous_promise_doesnt_fail() throws Exception {
when.resolve(input)
.orElse(resolved(sentinel))
.then((val) -> {
assertEquals(input, val);
return null;
})
.ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Override
public <T, U> Promise<U> when(T x, Function<T, ? extends Thenable<U>> onFulfilled, Function<Throwable, ? extends Thenable<U>> onRejected) {
Promise<T> p = resolve(x);
return p.then(onFulfilled, onRejected);
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testFold_should_reject_and_not_call_combine_if_promise_rejects() throws Exception {
when.<Integer>reject(sentinelEx)
.fold(noop, resolved(2))
.otherwise((e) -> {
assertEquals(sentinelEx, e);
return null;
})
.then(done.onFulfilled, done.onRejected);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_yield_should_fulfill_with_value_of_a_fulfilled_promise() {
when.resolve(other)
.yield(resolved(sentinel))
.then(
(value) -> {
assertEquals(sentinel, value);
return null;
}
)
.ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_tap_should_fulfill_with_the_original_value() {
when.resolve(sentinel)
.tap((x) -> {
return resolved(other);
})
.then((value) -> {
assertEquals(sentinel, value);
return null;
})
.ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testFold_should_pass_value_and_arg() throws Exception {
when.<Object>resolve(other)
.fold((a, b) -> {
assertEquals(sentinel, a);
assertEquals(other, b);
return null;
}, resolved(sentinel))
.then(done.onFulfilled, done.onRejected);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_otherwise_should_register_errback() {
when.reject(sentinelEx)
.otherwise(
(val) -> {
assertEquals(sentinelEx, val);
return null;
})
.ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_otherwise_should_return_a_promise() {
assertNotNull(when.defer().getPromise().otherwise(null));
}
代码示例来源:origin: ef-labs/vertx-cassandra
@Test
public void testReadyReject() throws Exception {
Promise<Void> p = whenCassandraSession.ready();
State<Void> state = p.inspect();
assertEquals(HandlerState.PENDING, state.getState());
verify(cassandraSession).onReady(onReadyCaptor.capture());
onReadyCaptor.getValue().handle(Future.failedFuture("Test fail"));
state = p.inspect();
assertEquals(HandlerState.REJECTED, state.getState());
}
代码示例来源:origin: ef-labs/when.java
@Override
public <T, U> Promise<U> when(Thenable<T> x, Function<T, ? extends Thenable<U>> onFulfilled, Function<Throwable, ? extends Thenable<U>> onRejected) {
Promise<T> p = resolve(x);
return p.then(onFulfilled, onRejected);
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testFold_should_reject_and_not_call_combine_if_arg_rejects() throws Exception {
when.resolve(1)
.fold(noop, when.reject(sentinelEx))
.otherwise((e) -> {
assertEquals(sentinelEx, e);
return null;
})
.then(done.onFulfilled, done.onRejected);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testElse_should_resolve_with_else_value_if_previous_promise_fails() throws Exception {
when.reject(new RuntimeException())
.orElse(resolved(sentinel))
.then((val) -> {
assertEquals(sentinel, val);
return null;
})
.ensure(done::fulfill);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/vertx-cassandra
@Test
public void testReady() throws Exception {
Promise<Void> p = whenCassandraSession.ready();
State<Void> state = p.inspect();
assertEquals(HandlerState.PENDING, state.getState());
verify(cassandraSession).onReady(onReadyCaptor.capture());
onReadyCaptor.getValue().handle(Future.succeededFuture());
state = p.inspect();
assertEquals(HandlerState.FULFILLED, state.getState());
}
代码示例来源:origin: ef-labs/vertx-jersey
@GET
@Produces(MediaType.TEXT_PLAIN)
public void getString(@Suspended AsyncResponse asyncResponse) throws IOException {
Deferred<String> d = when.defer();
d.getPromise()
.then(str -> {
asyncResponse.resume(str);
return null;
})
.otherwise(t -> {
asyncResponse.resume(new WebApplicationException(t));
return null;
});
d.resolve("when.java!");
}
代码示例来源:origin: ef-labs/when.java
/**
* try/lift helper that allows specifying thisArg
*/
private <T, U> Promise<U> _apply(Function<List<T>, Thenable<U>> f, List<Thenable<T>> args) {
return helper.all(args).then(f::apply);
// return Promise.all(args).then(function(args) {
// return f.apply(thisArg, args);
// });
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testFold_should_reject_if_combine_throws() throws Exception {
when.resolve(1)
.fold((x, y) -> {
throw sentinelEx;
}, resolved(2))
.otherwise((e) -> {
assertEquals(sentinelEx, e);
return null;
})
.then(done.onFulfilled, done.onRejected);
done.assertFulfilled();
}
代码示例来源:origin: ef-labs/when.java
@Test
public void testPromise_inspect_when_inspecting_promises_should_return_fulfilled_state_for_fulfilled_promise() {
Promise<Sentinel> promise = when.resolve(sentinel);
promise.then((x) -> {
assertFulfilled(promise.inspect(), sentinel);
return null;
}).ensure(done::fulfill);
done.assertFulfilled();
}
内容来源于网络,如有侵权,请联系作者删除!