我通过打字稿在 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 ]
然而,在我的代码库中,我有一些不返回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)
我知道我可以将我的 Promise 拒绝转换为 null 值并将compact
它们作为一种解决方法,但我宁愿处理 Promise 拒绝本身。
我怎样才能只处理成功的承诺流而忽略使用高地的失败承诺流?我应该如何处理错误事件?