Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created October 25, 2018 04:35
Show Gist options
  • Save jcuffe/d6b50bc8677ab9987c47ac8ff60472bc to your computer and use it in GitHub Desktop.
Save jcuffe/d6b50bc8677ab9987c47ac8ff60472bc to your computer and use it in GitHub Desktop.
Making 1000 requests without angering the remote server
const ordersEpic = (action$, state$) => action$.pipe(
// Listen for this action
ofType(type.GET_ORDERS),
// Replace the action with the list of active IDs
mapTo(state$.value.market.ids),
// Turn the list into a stream of ids
flatMap(from),
// Add a 1 second delay to each emit
concatMap(id => of(id).pipe(delay(1000))),
// Replace the id with the http response for orders of that id
switchMap(id => ajax(endpoints.orders(10000002, id))
.pipe(map(({ response }) => response))),
// Send the orders to state storage
map(setOrders)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment