简体   繁体   English

如何在highland.js地图中使用错误

[英]How to use errors in highland.js map

This function 这个功能

var _ = require('highland');
var accounts = ['ack', 'bar', 'foo'];

_(accounts).map(function (x) {
  return new Error(x);
}).errors(function (err, push) {
  push(null, 'fark');
}).each(function (x) {
  console.log(x);
});

logs 日志

[Error: ack]
[Error: bar]
[Error: foo]

I expected it to log 我希望它能记录下来

fark
fark
fark

How do I use errors correctly in highland.js? 如何在highland.js中正确使用错误?

Reading the source of the map function linked on http://highlandjs.org/#map you can see that the function is applied like this : 阅读http://highlandjs.org/#map map链接的map函数的来源,您可以看到该函数的应用如下:

var fnVal, fnErr;
try {
   fnVal = f(x);
} catch (e) {
    fnErr = e;
}
push(fnErr, fnVal);

so if you want to transform a value into a highland StreamError via the map transformer, you need to throw the error. 因此,如果您想通过map变换器将值转换为高地StreamError,则需要抛出错误。

The following code : 以下代码:

var _ = require('highland');
var accounts = ['ack', 'bar', 'foo'];

_(accounts).map(function (x) {
  throw new Error(x);
}).errors(function (err, push) {
  push(null, 'fark');
}).each(function (x) {
  console.log(x);
});

will give you the expected 会给你预期的

fark
fark
fark

In the documentation you have this : 文档中你有这个:

getDocument.errors(function (err, push) {
    if (err.statusCode === 404) {
        // not found, return empty doc
        push(null, {});
    }
    else {
        // otherwise, re-throw the error
        push(err);
    }
});

So I think your error still at the following line : 所以我认为你的错误仍然在以下几行:

push(null, 'fark');

try with 尝试

push(err);

Hope this help you ! 希望这对你有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM