繁体   English   中英

如何将函数的结果发送给第三个函数而不返回?

[英]How do I send the result of a function to a third one without a return?

我试图出于单元测试的目的在NodeJS服务中调用Cloudant映射函数。

我已经创建了自己的发射函数,但是仍然无法获得返回所需信息的视图。

const data = {
    "_id": "68e3d2807c0faa169d504068db99e03f",
    "_rev": "2-c3b3249a2eb090e87dc4c053712691c3",
    "type": "user",
    "name": "Whatever",
    "email": "Whatever@google.com",
    "cn": "5517605X",
    "phone": null,
    "deleted": true
}

const emit = function (arg1, arg2) {
    return { key: arg1, value: arg2 };
};

var map = function (doc) {
    if (doc.type && doc.type === 'user') {
        emit([doc.cn, doc.deleted], doc);
    }
};

const result = map(data);
console.log(result);

JSF只是我要尝试做的事情。 我知道我可以通过在emit函数前面添加一个返回值来解决此问题,但是由于要从Cloudant直接获取视图,因此我无法编辑map函数。

有什么办法可以使我继续工作吗?

但我无法编辑地图功能

这样就无法更改map函数的返回值。 调用它将始终导致undefined 除了更改map (或使用其他方法)之外,别无他法。


在特定的情况下,你已经证明, emit可以关闭了result ,并直接设置它,如果你移动的result宣布了上述emit ,并使它成为let

let result;
const emit = function (arg1, arg2) {
    result = { key: arg1, value: arg2 };
    return result;
};

var map = function (doc) {
    if (doc.type && doc.type === 'user') {
        emit([doc.cn, doc.deleted], doc);
    }
};

map(data);
console.log(result);

...但这是特定于该代码的,即使您不追求全面的功能编程方法,拥有类似副作用的功能也不是一件好事。 但有时需要。

暂无
暂无

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

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