redux saga 知道任务何时完成

redux saga knowing when task is done

我有以下故事:

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),
  ]);

}

我怎么知道 [stripeCustomerTask, stripeInvoicesTask] 何时完成?

我想在每个动作完成后执行另一个动作。

因为它在 all([...]) 中,而您已经屈服了,您可以直接将代码放在后面。在返回这些值之前,它之后的代码不会执行。你可以开始了!继续编码。

This is one of the best redux-sagas blogs I have read to date 很好地涵盖了这一点。这里的关键部分是:

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.

因此,您可以将代码紧跟在 all 之后,并期望获得这些值。

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