Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

node.js - 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();
    }

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose you could make it all nice and rxjs by using from and concatAll:

untested code

// 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();

This will make the calls consecutively. If it's possible to do the requests at the same time, you can do mergeAll().


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...