小编mjo*_*ble的帖子

处理错误后跳过承诺链

使用https://github.com/kriskowal/q库,我想知道是否可以这样做:

// Module A

function moduleA_exportedFunction() {
  return promiseReturningService().then(function(serviceResults) {
    if (serviceResults.areGood) {
      // We can continue with the rest of the promise chain
    }
    else {
      performVerySpecificErrorHandling();
      // We want to skip the rest of the promise chain
    }
  });
}

// Module B

moduleA_exportedFunction()
  .then(moduleB_function)
  .then(moduleB_anotherFunction)
  .fail(function(reason) {
    // Handle the reason in a general way which is ok for module B functions
  })
  .done()
;
Run Code Online (Sandbox Code Playgroud)

基本上,如果服务结果不好,我想使用特定于模块A内部的逻辑来处理模块A中的故障,但仍然跳过promise链中剩余的模块B函数.

跳过模块B函数的显而易见的解决方案是从模块A抛出错误/原因.但是,我需要在模块B中处理它.理想情况下,我想在模块B中不需要任何额外的代码来执行它.那.

这可能是不可能的:)或者反对Q的一些设计原则.

在这种情况下,您会建议什么样的替代方案?

我有两种方法,但都有它们的缺点:

  1. 从模块A抛出特定错误并将特定处理代码添加到模块B:

    .fail(function(reason) {
      if (reason …
    Run Code Online (Sandbox Code Playgroud)

javascript node.js promise q

8
推荐指数
2
解决办法
6904
查看次数

Using BouncyCastle for a simple HTTPS query

Here's a simplified version of the code I'm using to perform simple HTTPS requests:

// Assume the variables host, file and postData have valid String values

final URL url = new URL("https", host, file);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-length", String.valueOf(postData.length()));

final DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(postData);
output.close();

final InputStream input = new DataInputStream(connection.getInputStream());

for (int c = input.read(); c != -1; c = input.read()) {
  System.out.print((char) c);
}

System.out.println();

input.close();
Run Code Online (Sandbox Code Playgroud)

This used to work …

java ssl https bouncycastle jce

5
推荐指数
2
解决办法
2万
查看次数

为什么从不分配给每种类型?

TypeScript文档说

never类型是每种类型的子类型,并且可以分配给每种类型

但没有提及原因。

凭直觉,我希望这样的代码会失败:

const useString = (str: string) => console.log('This is definitely a string:', str)
const useNever = (not_a_string: never) => useString(not_a_string)
Run Code Online (Sandbox Code Playgroud)

但没有错误,因为任何never值都被视为有效字符串。

这是故意的吗?如果是,那为什么呢?:)

typescript

3
推荐指数
1
解决办法
165
查看次数

标签 统计

bouncycastle ×1

https ×1

java ×1

javascript ×1

jce ×1

node.js ×1

promise ×1

q ×1

ssl ×1

typescript ×1