我通过 typescript 在node@8.11.1上使用highland@2.13.0。鉴于此代码片段:
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 ]
然而,在我的代码库中,我的 promise 不会返回 null
值,但会拒绝该 promise 。但在这种情况下,高地崩溃:
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)
我知道我可以将 Promise 的拒绝转换为空值并压缩
它们作为解决方法,但我宁愿处理 Promise 拒绝本身。
我怎样才能使用高地只处理成功的 promise 流并忽略失败的 promise 流?我应该如何处理错误事件?
最佳答案
使用_.errors
方法:
Extracts errors from a Stream and applies them to an error handler function. Returns a new Stream with the errors removed (unless the error handler chooses to rethrow them using push). Errors can also be transformed and put back onto the Stream as values.
对于您的用例,这是最少需要的实现:
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
}
})
关于javascript - 从 promise 创建高地溪流时如何处理 promise 拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50328870/