E_someRandomString
E_someRandomString

Reputation: 51

Rxjs sequential API calls results with only one call

I do have such a code:

enum State {
    SUCCESS = 'success',
    ERROR = 'error'
}

const files: File[] = [...some files here];
const toBase64 = (file: File): Observable<string>; //this works 100% fine
const request = (base64: string) => service.upload(base64);

function buildRequest(file: File) {
    return toBase64(file).pipe(
        switchMap(base64 => this.request(base64)),
        map(() => State.SUCCESS),
        catchError((err) => {
            console.error(err);
            return of(State.ERROR);
        })
    );
}

from(files).pipe(
    concatMap(entry => buildRequest(entry)),
    scan((fileNo, result, index) => {
        // some code here
        return fileNo + 1;
    })
).subscribe();

Now, when every api calls return 400, all those calls are executed When first api call returns 200, only this call is executed, but the rest are not.

Upvotes: 0

Views: 40

Answers (1)

E_someRandomString
E_someRandomString

Reputation: 51

Seems some other function caused bad behavior (fileToBase64Observable) which I need to rework. Thank you all :)

Upvotes: 0

Related Questions