Sawtaytoes
12/12/2019 - 5:15 AM

Evert's Method

const fetchUserEpic = (
  action$,
) => (
  action$
  .pipe(
    ofType(FETCH_USER),
    switchMap(({
      password,
      username,
    }) => (
      ajax
      .getJSON(
        'https://api.example.com/login',
        {
          password,
          username,
        },
      )
      .pipe(
        map(({
          authToken,
        }) => (
          fetchUserSucceeded(
            authToken
          )
        )),
        startWith(
          setLoading(
            'login',
          )
        ),
        endWith(
          setLoaded(
            'login',
          )
        ),
        catchError((
          error,
        ) => (
          of(
            fetchUserFailed(
              error,
            )
          )
        )),
      )
    )),
  )
)