2

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?

Vegas Rico
  • 73
  • 9
  • @churill the code that I put above is replica of working code. It's still working, but with warning about deprecation. The given [documentation](https://rxjs.dev/deprecations/array-argument) for it, so I do as described there. Even when I subscribing, it isn't working. – Vegas Rico Jan 10 '23 at 13:43

1 Answers1

2

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.

DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • Hello Deborah. Thank you for your time and effort to help me with this issue, I really appreciate it. Unfortunately, that didn't help either. ` const setToHandle: Observable = []; ` Inside the doWorksAndGetResult function code that reaching to the db ` return from(this.typeORM.query('SELECT ....')) .pipe( switchMap((res) => { **console.log("I'm here!!!");** // do something this.results.push(some_variable); return of(); })); ` – Vegas Rico Jan 13 '23 at 11:51
  • It is not getting to **console.log("I'm here!!!")** – Vegas Rico Jan 13 '23 at 11:52
  • See my update above. – DeborahK Jan 13 '23 at 19:28
  • Hi Deborah. Please take a look [here](https://stackblitz.com/edit/angular-ivy-nkifwk?file=src/app/app.component.ts) – Vegas Rico Jan 15 '23 at 08:57
  • I am using the above link and don't see an issue? – DeborahK Jan 16 '23 at 17:23
  • Hi @Deborah. [here](https://stackblitz.com/edit/angular-ivy-nkifwk?file=src/app/app.component.ts) I'm altered code to show the issue. There is must be output in console (Line 24), but it doesn't. If you will change **return forkJoin([setToHandle]);** to **return forkJoin(setToHandle);** you will see the output (line 21). But it should work in this well too **return forkJoin([setToHandle]);** – Vegas Rico Jan 18 '23 at 09:00
  • See Update 2 above. – DeborahK Jan 18 '23 at 17:14
  • Thanks a lot Deborah!!! It's really works! – Vegas Rico Jan 19 '23 at 16:42