2

If you've the following effect

  testEffects$ = createEffect(() => this.actions$.pipe(
    ofType(RDX_TEST_INC_VALUE),
    switchMap(ac => instance.get('/api/test/kam').then(res => {
      return {
        type: RDX_TEST_INC_VALUE_SUCCESS,
      }
    }).catch((err: AxiosError) => {
      return {
        type: RDX_TEST_INC_VALUE_ERROR,
      }
    }))
  ))

How do we return the payload of RDX_TEST_INC_VALUE with RDX_TEXT_INC_VALUE_SUCCESS?

Noah Bergh
  • 493
  • 4
  • 14

2 Answers2

2

You can just use the payload

  testEffects$ = createEffect(() => this.actions$.pipe(
    ofType(RDX_TEST_INC_VALUE),
    switchMap(ac => instance.get('/api/test/kam').then(res => {
      return {
        type: RDX_TEST_INC_VALUE_SUCCESS,
        //  we can just grab the payload from the action
        payload: ac.payload
      }
    }).catch((err: AxiosError) => {
      return {
        type: RDX_TEST_INC_VALUE_ERROR,
      }
    }))
  ))
timdeschryver
  • 14,415
  • 1
  • 19
  • 32
0

Depending on exactly you want to achieve, something like this should work

  testEffects$ = createEffect(() => this.actions$.pipe(
    ofType(RDX_TEST_INC_VALUE),
    withLatestFrom( from(instance.get('/api/test/kam'))),
    map(([action, promsieResult] => {
      // do whatever you want here
    }))
    catchError((err: AxiosError) => {
      return {
        type: RDX_TEST_INC_VALUE_ERROR,
      }
    }))
  ))

If you want withLatestFrom or something else depends on your use case. See https://www.learnrxjs.io/learn-rxjs/operators/combination for reference. I also converted your promise into an observable via the from Operator as described in Convert Promise to Observable. Much easier to handle inside the stream.

PS: Do not use forkJoin, it will not work with an action observable stream because it never completes.

Loop
  • 480
  • 2
  • 9