Ponpon32
Ponpon32

Reputation: 2210

redux saga knowing when task is done

I have the following saga:

function* someSaga(action) {
  const { currentUser: { accountId } } = action;

  const accountBillingPlanEffect = yield call(getAccountBillingPlan, services['account-billing-plans'], accountId);
  const accountBillingPlanResponse = yield (accountBillingPlanEffect.payload.promise);
  const { stripeSubscriptionId } = accountBillingPlanResponse.data[0];

  const stripeSubscriptionEffect = yield call(getStripeSubscription, services['stripe/subscriptions'], stripeSubscriptionId);
  const stripeSubscriptionResponse = yield (stripeSubscriptionEffect.payload.promise);
  const { customer } = stripeSubscriptionResponse.data[0];

//The important part
  const [stripeCustomerTask, stripeInvoicesTask] = yield all([
    fork(getStripeCustomer, services['stripe/customers'], customer),
    fork(getStripeInvoices, services['stripe/invoices'], customer),
  ]);

}

How can I know when [stripeCustomerTask, stripeInvoicesTask] are done?

I would like to execute another action when each one of them is completed.

Upvotes: 0

Views: 846

Answers (1)

Nick Brady
Nick Brady

Reputation: 6592

Since it's in the all([...]), and you're yielding on it, you can just put your code immediately after. The code after it won't execute until those values are returned. You're good to go! just keep coding.

This is one of the best redux-sagas blogs I have read to date which covers this pretty nicely. The key part here being:

redux-saga provides the all effect, which takes an array of blocking effects and waits for all of them to complete before resuming with all results.

So, you can just put your code right after the all, and expect to have the values.

//The important part
const [stripeCustomerTask, stripeInvoicesTask] = yield all([
  fork(getStripeCustomer, services['stripe/customers'], customer),
  fork(getStripeInvoices, services['stripe/invoices'], customer),
]);
// keep coding
if (stripeCustomerTask) { ... }

Upvotes: 1

Related Questions