Underscore 和 Mongoose:使用文档作为外部函数的返回值
我正在尝试将 Mongoose 和 Underscore 一起使用,来做这样的事情:
var person_ids = [1, 2, 3];
var persons = _(person_ids).map(function(id) {
Person.findById(id, function(person) { // Non-blocking
// How do I use 'person' as the outer function's return value?
});
});
有什么办法可以做到这一点吗?我意识到我可能试图在设计为异步使用的库上强制采用同步范例。
I'm trying to use Mongoose and Underscore together, to do something like this:
var person_ids = [1, 2, 3];
var persons = _(person_ids).map(function(id) {
Person.findById(id, function(person) { // Non-blocking
// How do I use 'person' as the outer function's return value?
});
});
Is there any way to do this? I realize I may be trying to force a synchronous paradigm on a library designed to be used asynchronously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您获得价值的唯一方法是通过回调:
检查我的答案:
如何检索类中变量的值< /a>
代码中的重要位置是:
和
only way you can get value is by callback:
check my answer at:
How to retrieve value of a variable in class
important places in code are:
and