如何在从承诺创建高地流时处理承诺拒绝?

时间:2018-05-14 11:10:29

标签: javascript node.js typescript promise highland.js

我通过typescript在node@8.11.1上使用highland@2.13.0。鉴于此代码snipplet:

import * as highland from "highland";
import * as lodash from "lodash/fp";

const range = lodash.range(0, 10);
const createPromise = async (i: number): Promise<number> => {
    if (i % 2 !== 0) {
        return Promise.resolve(i);
    }
    return Promise.resolve(null);
};

highland(range).map((i) => {
        return highland(createPromise(i));
    })
    .flatten() // resolving the promises
    .compact() // removing the null values
    .toArray((items) => console.log(items));

它将返回我的预期输出:

[ 1, 3, 5, 7, 9 ]

然而,在我的代码库中,我承诺不会返回null值,但会拒绝承诺。在那种情况下,高地崩溃:

const createPromise = async (i: number): Promise<number> => {
    if (i % 2 !== 0) {
        return Promise.resolve(i);
    }
    return Promise.reject("Some rejection message");
};


highland(range).map((i) => {
        return highland(createPromise(i));
    })
    .flatten()
    .toArray((items) => console.log(items));

将抛出:

events.js:188
      throw err;
      ^

Error: Unhandled "error" event. (Invalid)
    at Stream.emit (events.js:186:19)
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1908:18
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1593:9
    at Stream.s._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1549:9)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at Stream._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:974:26)
    at push (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1515:19)
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:3918:13
    at Stream.s._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1549:9)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at Stream._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:974:26)
    at push (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1515:19)
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:2458:13
    at Stream.s._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1549:9)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at Stream._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:974:26)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:680:15
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:3606:17
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1593:9
    at Stream.s._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1549:9)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at Stream._send (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:974:26)
    at Stream.write (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:1647:18)
    at /home/philipp/rate-pipeline/node_modules/highland/lib/index.js:680:15
    at Immediate._onImmediate (/home/philipp/rate-pipeline/node_modules/highland/lib/index.js:541:17)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)

我知道我可以将我的承诺拒绝转换为空值并compact将它们作为一种解决方法,但我宁愿处理承诺拒绝本身。

我如何只使用成功的Promises流并忽略使用highland的失败的流?我该如何处理错误事件?

1 个答案:

答案 0 :(得分:1)

使用_.errors方法:

  

从Stream中提取错误并将其应用于错误处理函数。返回删除了错误的新Stream(除非错误处理程序选择使用push重新抛出它们)。错误也可以转换并作为值返回到Stream。

对于您的用例,这是最不需要的实现:

highland(range).map((i) => {
    return highland(createPromise(i));
  })
  .flatten()
  .errors(() => {})
  .toArray((items) => console.log(items));

它将输出:

[ 1, 3, 5, 7, 9 ]

可以对错误采取行动并将自定义值返回到流中或重新抛出错误:

.errors((error, push) => {
   if(error.foo === "bar") {
     push(null, null); // pushes null to the result stream
   } else {
     push(err); // re-throws
   }
})