Search code examples
rxjsnestjsrxjs6rxjs-observablesrxjs-pipeable-operators

forkJoin on NestJS not working after migration to newer version


I'm running nestjs application and have a peace of code that using forkJoin

const results: string[] = [];
const args = ['arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6'];

....
switchMap(() => {
   const setToHandle = [];
   args.forEach(arg => setToHandle.push(this.doWorksAndGetResult(arg)));
   return forkJoin(setToHandle);
}),
tap(() => {
   this.logService.debug(...results);
})

So this work fine, and I got results printed to log. But this forkJoin(setToHandle) is deprecated now and should use like that forkJoin([setToHandle]) according the documentation. But it isn't work, no results printed in log, no observables called inside doWorksAndGetResult function. Can some one help me with it?


Solution

  • After adding enough code to get your example to work, I found that strongly typing setToHandle fixed the issue. (At least in my version of your code.)

    const setToHandle: string[] = [];
    

    Otherwise, I think that the language service is getting confused.

    UPDATE based on your comments:

    This initialization is not valid:

    const setToHandle: Observable<void> = []; 
    

    You can't initialize an Observable<void> to an empty array []. And you can't then later in your code push to an Observable<void>

    Could you provide enough working code for us to get a better idea of what you are trying to do. See this Stackblitz for a place to start: https://stackblitz.com/edit/angular-ivy-yp3ocd

    UPDATE 2 based on your later comments:

    forkJoin() takes in an array.

    const setToHandle: Observable<boolean>[] = [];
    

    setToHandle is already defined as an array.

    Passing in [setToHandle] is putting the array into another array.

    If you really need to use the square brackets, then this works:

    return forkJoin([...setToHandle]);
    

    The spread operator (...) expands the array.