0

Example in next.js repository shows how we can convert root epic to Promise, so it's possible to await async action like ajax:

static async getInitialProps ({ store, isServer }) {
  const resultAction = await rootEpic(
    of(actions.fetchCharacter(isServer)),
    store
  ).toPromise() // we need to convert Observable to Promise
  store.dispatch(resultAction)

  return { isServer }
}

I wonder though how it would be possible to await more than one action. I managed to do it this way:

const actions = await Promise.all([
  rootEpic(of(fetchCharacterOne()), store).toPromise(),
  rootEpic(of(fetchCharacterTwo()), store).toPromise(),
  rootEpic(of(fetchCharacterThree()), store).toPromise(),
]);

actions.forEach(store.dispatch);

...but I wonder if there is a simpler way than calling rootEpic each time - I mean call it once with three actions to await.

1 Answer 1

1

I've found the answer accidentally in universal-rxjs-ajax docs. So that's the code I was looking for:

const initialData$ = of(
  fetchCharacterOne(),
  fetchCharacterTwo(),
  fetchCharacterThree(),
);

const actions = await rootEpic(initialData$, store)
  .pipe(toArray()) // buffers all emitted actions until Epic completes
  .toPromise();

actions.forEach(store.dispatch);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.