I am trying to wrap my head around the difference between concat and concatMap when using redux-observable.
In my intuition, I'm thinking that concatMap would work:
- From a FAKE_LOGIN
action, it will be switchMap-ed to emit FAKE_LOGIN_AUTHENTICATING
action.
- Inside a FAKE_LOGIN_AUTHENTICATING
action, it will be concatMap-ed to emit FAKE_LOGIN_SUCCESS
action, after a delay of 2 seconds.
However, the above thinking does not work. I tried all sorts of combinations and finally chanced upon the concat operator and surprisingly it works.
What is the difference between the two?
Below are my codes:
this does not work
action$.pipe(
switchMap(
action => of( { type: 'FAKE_LOGIN_AUTHENTICATING' } ).pipe(
concatMap( thing => {
return of( { type: 'FAKE_LOGIN_SUCCESS', payload: { userId: 'user-a', userData: {} } } ).pipe(
delay( 2000 )
);
} )
)
)
);
But this does
action$.pipe(
switchMap(
action => of( { type: 'FAKE_LOGIN_AUTHENTICATING' } ).pipe(
concat(
of( { type: 'FAKE_LOGIN_SUCCESS', payload: { userId: 'user-a', userData: {} } } ).pipe(
delay( 2000 )
)
)
)
)
);