0

I have my resolver.ts

return this.wardrobeStateServ.init(this.categoryIndexes).pipe(take(1),
  map((value) => value));

and my init function

    init(categoryIndexes: number[]): Observable<boolean> {
    if (this.firstSubWardrobeSubject.value === null) {
        return this.http.get(`${AppConstants.itemsByCategoriesUrl}${categoryIndexes[0]}&include_images=true`).pipe(switchMap((firstResponse: any) => {
            this.firstSubWardrobeSubject.next(firstResponse.data);
            return this.http.get(`${AppConstants.itemsByCategoriesUrl}${categoryIndexes[1]}&include_images=true`).pipe(switchMap((secondResponse: any) => {
                this.secondSubWardrobeSubject.next(secondResponse.data);
                return this.http.get(`${AppConstants.itemsByCategoriesUrl}${categoryIndexes[2]}&include_images=true`).pipe(switchMap((thirdResponse: any) => {
                    this.thirdSubWardrobeSubject.next(thirdResponse.data);
                    if (categoryIndexes[3]) {
                        return this.http.get(`${AppConstants.itemsByCategoriesUrl}${categoryIndexes[3]}&include_images=true`).pipe(map((forthResponse: any) => {
                            this.forthSubWardrobeSubject.next(forthResponse.data);
                            return true;
                        }))
                    } else { return of(true) }
                }))
            }))
        }))
    } else { return of(true) }
}

Do you have any idea to simplify my code please? I want to have an array where I can loop with my http request.

But I tried with forkJoin but there is an asyn issue because result is always null

So now, this function is working but not so clean.

Help me please

  • If i understand correctly you want for each categoryIndexes to call the same http get request with the corresponding categoryIndexes value? If yes, how will you know in each loop which Subject to use to next()? I am a bit confused regarding these Subjects. – tufonas Feb 10 '22 at 02:26
  • I will create a map with key: categoryIndex and value: the subjects that is the response – Ludovic Noel Feb 10 '22 at 08:11
  • 1
    You can try something like this `let mapObj = new Map(); of(categoryIndexes) .pipe(mergeMap((value) => value)) .pipe(switchMap((res) => zip(of(res), of('Your http get request')))) .pipe(toArray()) .pipe(switchMap( res => { res.forEach( val => mapObj.set(val[0],val[1])) return of(mapObj); })) .subscribe((res) => console.log(res));` I dont post it as an answer cause i dont know if it is what you want. – tufonas Feb 10 '22 at 09:07
  • There is last issue, it is that we want map with a subject/behaviorSubject and not an observable.. – Ludovic Noel Feb 10 '22 at 13:39
  • You can modify the above to play with Subjects/BehaviourSubjects – tufonas Feb 10 '22 at 13:49
  • I tried but when I put subscribe(()), I lost my data and go directly to my resolver without data. – Ludovic Noel Feb 10 '22 at 15:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241909/discussion-between-tufonas-and-ludovic-noel). – tufonas Feb 10 '22 at 15:54

0 Answers0