Sawtaytoes
12/6/2018 - 3:45 PM

Race to the State - Correct Usage

This is the correct implementation when grabbing values off the state.

const storeInventoryItemsEpic = (
  action$,
  state$,
) => (
  action$
  .pipe(
    ofType(FETCH_INVENTORY_ITEMS),
    switchMap(
      state$
      .pipe(
        map(accessTokenSelector),
        filter(Boolean),
        take(1),
        switchMap(accessToken => (
          ajax({
            crossDomain: true,
            headers: {
              'Authorization': `Bearer ${accessToken}`,
            },
            url: 'https://example.com/inventory-items',
          })
          .pipe(
            pluck('response'),
            map(storeInventoryItems),
          )
        )),
      )
    )
  )
)