使用 mongoose 完成两个异步查询后进行回调
使用猫鼬,我希望在两个不同的查询完成后进行回调。
var team = Team.find({name: 'myteam'}); var games = Game.find({visitor: 'myteam'});
那么假设我希望这些请求非阻塞并异步执行,如何在 Promise 中链接和/或包装这两个请求?
我想避免以下阻塞代码:
team.first(function (t) { games.all(function (g) { // Do something with t and g }); });
Using mongoose, I would like having a callback after 2 different queries have completed.
var team = Team.find({name: 'myteam'}); var games = Game.find({visitor: 'myteam'});
Then how to chain and/or wrap those 2 requests within promises assuming I want those requests non blocking and executed asynchronously?
I would like to avoid the following blocking code:
team.first(function (t) { games.all(function (g) { // Do something with t and g }); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你已经找到了解决方案,但无论如何。您可以轻松使用 async 库。在这种情况下,您的代码将如下所示:
I think you already found solution but anyway. You can easily use async library. In this case your code will looks like:
我想你想看看类似
https://github.com/creationix/step
I think you want to look at something like
https://github.com/creationix/step