Skip to content

Commit

Permalink
fixed serialResolve
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee348 committed Nov 11, 2024
1 parent 382206e commit fbdae29
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/sessions/src/trackers/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ export function raceUntil<T>(promises: Promise<T>[], fallback: T, evalRes: (val:
})
}

export function serialResolve<T>(promises: Promise<T>[], fallback: T, evalRes: (val: T) => boolean): Promise<T | undefined> {
return new Promise(resolve => {
for (const p of promises) {
p.then(val => {
if (evalRes(val)) resolve(val)
})
export async function serialResolve<T>(promises: Promise<T>[], fallback: T, evalRes: (val: T) => boolean): Promise<T> {
for (const p of promises) {
try {
const val = await p
if (evalRes(val)) {
return val
}
} catch {
// Continue to next promise if this one fails
continue
}
resolve(fallback)
})
}
return fallback
}

export async function allSafe<T>(promises: Promise<T>[], fallback: T): Promise<T[]> {
Expand Down

0 comments on commit fbdae29

Please sign in to comment.