mirror of
https://github.com/portainer/portainer.git
synced 2025-08-07 14:55:27 +02:00
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
This commit is contained in:
parent
da010f3d08
commit
ea228c3d6d
276 changed files with 9241 additions and 3361 deletions
|
@ -10,6 +10,46 @@ export function promiseSequence(promises: (() => Promise<unknown>)[]) {
|
|||
);
|
||||
}
|
||||
|
||||
type AllSettledItems<T> = {
|
||||
fulfilledItems: T[];
|
||||
rejectedItems: { item: T; reason?: string }[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Separates a list of items into successful and rejected items based on the results of asynchronous operations.
|
||||
* This function is useful for deleting in parallel, or other requests where the response doesn't have much information.
|
||||
*
|
||||
* @template T - The type of the items in the list.
|
||||
* @param {T[]} items - The list of items to process.
|
||||
* @param {(item: T) => Promise<void>} asyncFn - An asynchronous function that takes the item and performs an operation on it.
|
||||
* @returns {Promise<AllSettledItems<T>>} - A promise that resolves to an object containing arrays of successful and rejected items.
|
||||
*/
|
||||
export async function getAllSettledItems<T>(
|
||||
items: T[],
|
||||
asyncFn: (item: T) => Promise<void>
|
||||
): Promise<AllSettledItems<T>> {
|
||||
const results = await Promise.allSettled(items.map((item) => asyncFn(item)));
|
||||
|
||||
// make an array of successful items and an array of rejected items
|
||||
const separatedItems = results.reduce<AllSettledItems<T>>(
|
||||
(acc, result, index) => {
|
||||
if (result.status === 'fulfilled') {
|
||||
acc.fulfilledItems.push(items[index]);
|
||||
} else {
|
||||
const reason =
|
||||
result.reason instanceof Error
|
||||
? result.reason.message
|
||||
: String(result.reason);
|
||||
acc.rejectedItems.push({ item: items[index], reason });
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{ fulfilledItems: [], rejectedItems: [] }
|
||||
);
|
||||
|
||||
return separatedItems;
|
||||
}
|
||||
|
||||
export function isFulfilled<T>(
|
||||
result: PromiseSettledResult<T>
|
||||
): result is PromiseFulfilledResult<T> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue