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; }; 
3
  • 1
    have you looked at concatMap() ? Commented Nov 28, 2019 at 4:53
  • How would I use it? Call it recursively? Commented Nov 28, 2019 at 4:59
  • have you tried this answer - stackoverflow.com/questions/57784571/… Commented Nov 28, 2019 at 5:03

2 Answers 2

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 ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers, using from is the part I wasn't getting right
0

What I have done is

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.