简体   繁体   English

并行运行 HTTP 中的请求 Meteor

[英]Running parallel HTTP Requests in Meteor

Let's say I have a collection of documents that need to be updated from an external API. On the client, I fetch these docs and then delegate the call to the API on the Server in a Meteor method假设我有一组文档需要从外部 API 更新。在客户端上,我获取这些文档,然后使用 Meteor 方法将调用委托给服务器上的 API

Ie IE

//on client
const docs = Docs.find().fetch();
for (let doc of docs) {
  //Delegate to Server to handle actual request to external API and updating to DB
  Meteor.call('updateFromExternalAPI', doc);
}

I'm aware this is not efficient and I'd like to run these requests in parallel.我知道这效率不高,我想并行运行这些请求。 I've used Promises.all() before and understand there's probably a myriad of ways to do this in standard JS and other frameworks.我以前使用过Promises.all()并且了解在标准 JS 和其他框架中可能有无数种方法可以做到这一点。 But, what's the desired pattern for doing things like this in Meteor?但是,在 Meteor 中执行此类操作所需的模式是什么? Will I have to rely on other npm packages?我必须依赖其他 npm 包吗?

Any contemporary blogs, tutorials would be greatly appreciated.任何当代博客、教程将不胜感激。

Thanks!谢谢!

I'd like to run these requests in parallel我想并行运行这些请求

Without seeing your server code, I guess the only thing lacking to make the code fully parallel is this.unblock() at the beginning of the updateFromExternalAPI method.在没有看到您的服务器代码的情况下,我想使代码完全并行的唯一不足是updateFromExternalAPI方法开头的this.unblock() This would allow the next method to be called immediately, instead of waiting for the previous method call to return.这将允许立即调用下一个方法,而不是等待前一个方法调用返回。

what's the desired pattern for doing things like this in Meteor?在 Meteor 中执行此类操作所需的模式是什么?

Ideally, you would edit updateFromExternalAPI (or make a similar method) to take all docs at the same time and do the iteration itself.理想情况下,您将编辑updateFromExternalAPI (或制作类似的方法)以同时获取所有docs并自行进行迭代。

Even better, that method would .fetch() the data instead of receiving the complete docs from the client.更好的是,该方法将.fetch()数据而不是从客户端接收完整的文档。 The server should already have access to those anyway.无论如何,服务器应该已经可以访问那些。 If necessary, the server could filter based on criteria sent from the client (eg a list of _id s).如有必要,服务器可以根据客户端发送的标准进行过滤(例如_id列表)。

Does that help?这有帮助吗?

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

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