fix: ensure order is preserved in Promise.all of adjustments #5922
fix: ensure order is preserved in Promise.all of adjustments #5922
Conversation
Signed-off-by: yuda <yuda@megazone.com>
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
2 Skipped Deployments
|
|
🎉 @skdud4659 has been randomly selected as the reviewer! Please review. 🙏 |
|
✅ There are no commits in this PR that require review. |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the processing of adjustment policies and adjustments to enforce sequential execution, thereby ensuring order preservation.
- Sequentially processes policy updates/creations using a promise chain
- Processes adjustments in order via a nested promise chain
| await formPolicies.value.reduce(async (promise, policy, idx) => { | ||
| await promise; | ||
| if (policy.id.startsWith('rap-')) { | ||
| return updateAdjustmentPolicy(policy, idx); | ||
| } | ||
| return createAdjustmentPolicy(policy, idx); | ||
| }); | ||
| await Promise.all(policyPromises); | ||
| }, Promise.resolve()); | ||
|
|
||
| // CUD Adjustment | ||
| await deleteAdjustment(deletedPolicyIds); | ||
| const adjustmentPromises = formPolicies.value.flatMap(async (policy) => { | ||
| await formPolicies.value.reduce(async (promise, policy) => { | ||
| await promise; | ||
| const adjustments = formAdjustments.value.filter((adjustment) => adjustment.policyId === policy.id); | ||
| return adjustments.map(async (adjustment, idx) => { | ||
| return adjustments.reduce(async (adjPromise, adjustment, idx) => { | ||
| await adjPromise; | ||
| if (adjustment.id.startsWith('ra-')) { | ||
| return updateAdjustment(adjustment, idx); | ||
| } | ||
| return createAdjustment(adjustment, idx); | ||
| }); | ||
| }); | ||
| const resolvedAdjustments = await Promise.all(adjustmentPromises); | ||
| await Promise.all(resolvedAdjustments.flat()); | ||
| }, Promise.resolve()); | ||
| }, Promise.resolve()); |
There was a problem hiding this comment.
[nitpick] Using the reduce method for chaining promises can be less intuitive; consider using an explicit for-of loop for sequential processing to improve readability and maintainability.
| await formPolicies.value.reduce(async (promise, policy, idx) => { | ||
| await promise; | ||
| if (policy.id.startsWith('rap-')) { | ||
| return updateAdjustmentPolicy(policy, idx); | ||
| } | ||
| return createAdjustmentPolicy(policy, idx); | ||
| }); | ||
| await Promise.all(policyPromises); | ||
| }, Promise.resolve()); | ||
|
|
||
| // CUD Adjustment | ||
| await deleteAdjustment(deletedPolicyIds); | ||
| const adjustmentPromises = formPolicies.value.flatMap(async (policy) => { | ||
| await formPolicies.value.reduce(async (promise, policy) => { | ||
| await promise; | ||
| const adjustments = formAdjustments.value.filter((adjustment) => adjustment.policyId === policy.id); | ||
| return adjustments.map(async (adjustment, idx) => { | ||
| return adjustments.reduce(async (adjPromise, adjustment, idx) => { | ||
| await adjPromise; | ||
| if (adjustment.id.startsWith('ra-')) { | ||
| return updateAdjustment(adjustment, idx); | ||
| } | ||
| return createAdjustment(adjustment, idx); | ||
| }); | ||
| }); | ||
| const resolvedAdjustments = await Promise.all(adjustmentPromises); | ||
| await Promise.all(resolvedAdjustments.flat()); | ||
| }, Promise.resolve()); |
There was a problem hiding this comment.
[nitpick] Using reduce to iterate over adjustments for sequential promise resolution may obscure the intent; a for-of loop might provide clearer control flow, making the code easier to understand and maintain.
Signed-off-by: yuda <yuda@megazone.com>
|
✅ There are no commits in this PR that require review. |
Skip Review (optional)
style,chore,ci,test,docs)Description (optional)
Things to Talk About (optional)