I have a list containing parameters, such as:
[
{a:1a,b:1b,c:1c},
{a:2a,b:2b,c:2c},
{a:3a,b:3b,c:3c}
]
For each parameter object, I aim to initiate three consecutive API calls, each with specific values. Subsequently, the program should wait for the calls to complete in sequence – API 1 called with values in 'a', API 2 with values in 'b', and API 3 with values in 'c'.
These calls should occur one after the other, and they must be cancelable upon user action from UI button.
For instance, if the user cancels the second call while the first one is running, the second call should be terminated. Upon completion of one call, the next call should proceed to the third one.
How can this be achieve ?
I tried using Rxjs, but was not sure now to implement cancelable action from UI
You can use a combination of
switchMapwhich makes thing cancelable andtakeUntilwith a "cancel" observable which can let you cut off an observable stream. For your parameters, you can loop over them and use a structure like this:You can modify building that final object up as you need, but subscribing to
calls$(or converting to aPromiseandawaiting it) will net you an object like{first: ..., second: ..., third: ...}this.cancelHttpCalls$should be an observable on your service and you can funnel clicks to callservice.cancelHttpCalls$.next()which will cancel it.Set it up or rechain as you like, (also add error handling) but that should get you going.