0

Our service times out if we post too many items at the same time, what I am looking for is the RxJs operator that will allow me to serialise the post into multiple posts, something like

merge(
  chunk(items, 50).map(
    chunkItems => this.http.post('endPoint', chunkItems)
  )
).pipe(
  scan((errors, current) => [...errors, ...current], []);
)

But I don't want them to run in parallel like merge will, I want it to be like a switchMap that serialises the calls.

chunk is a function that breaks an array up into smaller chunks

export const chunk = (arr, len) => {
  let chunks = [],
    i = 0,
    n = arr && arr.length;

  while (i < n) {
    chunks.push(arr.slice(i, (i += len)));
  }

  return chunks;
};
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60

2 Answers2

1

Suppose you have dataList type of array with agreement count of 3, Try this.

dataList=[data1,data2,data3]
const agreement =3
from(dataList).pipe(mergeMap(request=>this.http
    .post(this.url + this.saveRequestUrl, request, this.options),null,agreement )
0

What I have done is

from(chunk(items, 50)).pipe(
  concatMap(chunkItems => this.http.post('endPoint', chunkItems)),
  scan((errors, current) => [...errors, ...current], [])
);
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60