如何保持http通话一段时间? [英] How can I hold on http calls for a while?
问题描述
我遇到以下问题,如果有人可以给我发送一个主意,我已经尝试了一些,但没有用,我将不胜感激. 考虑代码:
I have the following problem and I would appreciate if someone could send me an idea, I have tried some, but it did not work. Consider the code:
while (this.fastaSample.length > 0) {
this.datainputService
.saveToMongoDB(this.fastaSample.slice(0, batch))
.subscribe();
}
它想解决的问题是我无法在单个http调用中发送数据,因为它太大了,所以我能够发送10%的数据而没有任何问题,而且还不行! 所以我想,我应该发送较小的批次,并且我已经在这里咨询过one Q& A,他们为我提供了帮助,但是并没有解决问题.
It supposes to solve the issue that I cannot send my data in a single http call, since it is too big, I was able to send 10% without issue, more than that, it does not work! So I thought, I should send smaller batches, and I have consulted sone Q&As here, and they helped me, but did not solve the problem.
我试图像在节点中那样使用await,但是它不起作用;它会一次发送所有的http,最好停止/保留代码,直到最后一个http调用完成,这将是一个不错的选择! 有什么建议吗?
I have tried to use await as I did in node, but it does not work; it sends all the http at once, it would be nice to stop/hold the code until the last http call is complete, that would be nice! Any suggestion?
推荐答案
我想您可以通过使用from
和concatAll
将其与rxjs完美结合:
I suppose you could make it all nice and rxjs by using from
and concatAll
:
未测试的代码
// first create batches by chunking the array
const batches = Array.from(
{ length: Math.ceil(fastaSample.length / batch) },
(v, i) => fastaSample.slice(i * batch, i * batch + batch)
)
// Second go over these chunks using `from` and `concatAll`:
from(batches).pipe(
map((batch) => this.data.inputService.saveToMongoDB(batch)),
concatAll()
).subscribe();
这将连续拨打电话.如果可以同时执行请求,则可以执行mergeAll()
.
This will make the calls consecutively. If it's possible to do the requests at the same time, you can do mergeAll()
.
但是就像@Mike所评论的那样,似乎该问题应在MongoDB后端中处理并接受多部分请求.这样一来,您无需对内容进行分块
But like @Mike commented, it seems like the issue should be handled in the MongoDB backend and accept a multipart request. This way you don't need to chunk stuff
这篇关于如何保持http通话一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!