com.englishtown.promises.Promise.otherwise()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(308)

本文整理了Java中com.englishtown.promises.Promise.otherwise()方法的一些代码示例,展示了Promise.otherwise()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Promise.otherwise()方法的具体详情如下:
包路径:com.englishtown.promises.Promise
类名称:Promise
方法名:otherwise

Promise.otherwise介绍

[英]Add Error-type and predicate matching to catch. Examples: promise.catch(TypeError, handleTypeError) .catch(predicate, handleMatchedErrors) .catch(handleRemainingErrors)
[中]添加要捕获的错误类型和谓词匹配。例子:承诺。catch(TypeError,handleTypeError)。catch(谓词,handleMatchedErrors)。接住(手把手)

代码示例

代码示例来源: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/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/vertx-jersey

protected void init() throws Exception {
  locator.init(vertx);
  HttpClientOptions clientOptions = new HttpClientOptions()
      .setConnectTimeout(1000);
  httpClient = vertx.createHttpClient(clientOptions);
  CompletableFuture<String> future = new CompletableFuture<>();
  locator.deployJerseyVerticle()
      .then(id -> {
        future.complete(id);
        return null;
      })
      .otherwise(t -> {
        future.completeExceptionally(t);
        return null;
      });
  deploymentID = future.get(10, TimeUnit.SECONDS);
}

代码示例来源:origin: ef-labs/when.java

@Test
public void testPromise_otherwise_should_return_a_promise() {
  assertNotNull(when.defer().getPromise().otherwise(null));
}

代码示例来源:origin: ef-labs/when.java

@Test
public void testFlow_otherwise_when_predicate_is_provided_and_is_an_Error_type_match_should_only_catch_errors_of_same_type() throws Exception {
  IllegalArgumentException e1 = new IllegalArgumentException();
  rejected(e1)
      .otherwise(ClassCastException.class, fail.onRejected)
      .<Sentinel>otherwise(IllegalArgumentException.class,
          (e) -> {
            assertEquals(e1, e);
            return null;
          })
      .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 testFlow_otherwise_when_predicate_is_provided_and_is_a_predicate_function_should_only_catch_errors_of_same_type() throws Exception {
  IllegalArgumentException e1 = new IllegalArgumentException();
  rejected(e1)
      .otherwise(
          (e) -> {
            return e != e1;
          },
          fail.onRejected)
      .<Sentinel>otherwise(
          (e) -> {
            return e == e1;
          },
          (e) -> {
            assertEquals(e1, e);
            return null;
          })
      .then(done.onFulfilled, done.onRejected);
  done.assertFulfilled();
}

代码示例来源:origin: ef-labs/vertx-jersey

private void runTest(String role, int status) {
  getWhenHttpClient().request(HttpMethod.GET, 8080, "localhost", BASE_PATH + role)
      .then(response -> {
        assertEquals(status, response.statusCode());
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源: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 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 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/vertx-jersey

@Test
public void testGet() throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH)
      .then(response -> {
        assertEquals(200, response.statusCode());
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

@Test
public void testGet() throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, "http://localhost:8080/when")
      .then(response -> {
        assertEquals(200, response.statusCode());
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/when.java

@Test
public void testFold_should_reject_if_combine_returns_rejection() throws Exception {
  when.resolve(1)
      .fold((x, y) -> when.reject(sentinelEx), resolved(sentinel))
      .otherwise((e) -> {
        assertEquals(sentinelEx, e);
        return null;
      })
      .then(done.onFulfilled, done.onRejected);
  done.assertFulfilled();
}

代码示例来源:origin: ef-labs/vertx-jersey

private void runTest(String additionalPath, Consumer<Buffer> assertMethod) throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + additionalPath, new RequestOptions().setPauseResponse(true))
      .then(response -> {
        assertEquals(200, response.statusCode());
        return getWhenHttpClient().body(response);
      })
      .then(body -> {
        assertMethod.accept(body);
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

private void runTest(String additionalPath, Consumer<Buffer> assertMethod) throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + additionalPath, new RequestOptions().setPauseResponse(true))
      .then(response -> {
        assertEquals(200, response.statusCode());
        return getWhenHttpClient().body(response);
      })
      .then(body -> {
        assertMethod.accept(body);
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

@Test
public void testGet() throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH)
      .then(response -> {
        assertEquals(200, response.statusCode());
        String header = response.getHeader(PoweredByResponseFilter.HEADER_X_POWERED_BY);
        assertEquals(PoweredByResponseFilter.HEADER_X_POWERED_BY_VALUE, header);
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

@Test
public void testGet_OK() throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH, new RequestOptions().setPauseResponse(true))
      .then(response -> {
        assertEquals(200, response.statusCode());
        return getWhenHttpClient().body(response);
      })
      .then(body -> {
        String str = body.toString();
        assertEquals(str, TestResource.RESPONSE_BODY);
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

@Test
public void testGet_OK() throws Exception {
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH, new RequestOptions().setPauseResponse(true))
      .then(response -> {
        assertEquals(200, response.statusCode());
        return getWhenHttpClient().body(response);
      })
      .then(body -> {
        String str = body.toString();
        assertEquals(str, "Hello World!!! Hello!");
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

代码示例来源:origin: ef-labs/vertx-jersey

@Test
public void testExceptionMapper() throws Exception {
  RequestOptions options = new RequestOptions().setPauseResponse(true);
  getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + "/exception", options)
      .then(response -> {
        assertEquals(GuiceExceptionMapper.STATUS_IM_A_TEAPOT.getStatusCode(), response.statusCode());
        assertEquals(MediaType.APPLICATION_JSON, response.getHeader(HttpHeaders.CONTENT_TYPE));
        return getWhenHttpClient().body(response);
      })
      .then(body -> {
        JsonObject json = body.toJsonObject();
        assertTrue(json.containsKey("msg"));
        assertTrue(json.containsKey("type"));
        testComplete();
        return null;
      })
      .otherwise(this::onRejected);
  await();
}

相关文章