我有以下传奇故事:
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([...])
中,并且您正在对它让步,因此您可以立即将代码放在它之后。在返回这些值之前,它之后的代码不会执行。你可以走了!继续编码。
这是迄今为止我读过的最好的redux-sagas
博客之一,它很好地涵盖了这一点。这里的关键部分是:
redux传奇提供了
all
效果,它采用了一系列阻塞效果,并等待所有这些效果完成,然后再恢复所有结果。
因此,您可以将代码放在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) { ... }