我希望能够遍历对象数组,获取一个 ID,然后为每个 ID 触发一个动作。我不希望它们一次全部触发,因为当我这样做时,这些操作中的每一个都会发出另一个 http 请求(来自另一个传奇)并达到我的 API 速率限制。所以如果我们看看我的代码:
try {
const membershipState: MembershipsState = yield select(selectMembershipsState);
let memberships = membershipState.memberships || [];
if (!memberships?.length || isDataExpired(membershipState.lastFetchedAt) || action.payload) {
const { data } = yield call(() => getMemberships());
memberships = data;
const relatedAddons = memberships.map((m: Membership) => put(getAddOnsRequested(m.id)));
const relatedMemberships = memberships.map((m: Membership) => put(getUpsellMembershipsRequested(m.id)));
yield all([
put<any>(getMembershipsSuccess(memberships)),
put(setLastFetchedAt()),
...relatedAddons,
...relatedMemberships,
]);
return;
}
对于相关插件和相关会员,我想交错开火。我想我需要从所有效果中取出动作并以某种方式循环它们并延迟触发每个动作。我无法遍历数组,因为在迭代中使用的函数意味着 yield 在直接生成器范围之外。 我试过油门和延迟效果,但没有成功。谁能建议我怎么做?