Skip to content

Commit

Permalink
fix typescript issue with generic tryit wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 23, 2024
1 parent c6280a0 commit f425a44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/async/defer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function defer<TResponse>(
fn: (error?: any) => any
rethrow: boolean
}[] = []

const register = (
fn: (error?: any) => any,
options?: { rethrow?: boolean },
Expand All @@ -39,15 +40,22 @@ export async function defer<TResponse>(
fn,
rethrow: options?.rethrow ?? false,
})
const [err, response] = await tryit(func)(register)

const [err, response] = await tryit<
Parameters<typeof func>,
Promise<unknown>,
unknown
>(func)(register)

for (const { fn, rethrow } of callbacks) {
const [rethrown] = await tryit(fn)(err)
if (rethrown && rethrow) {
throw rethrown
}
}

if (err) {
throw err
}
return response
return response as TResponse
}
6 changes: 5 additions & 1 deletion src/async/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export async function parallel<T, K>(
if (!next) {
return res(results)
}
const [error, result] = await tryit(func)(next.item)
const [error, result] = await tryit<
Parameters<typeof func>,
Promise<unknown>,
unknown
>(func)(next.item)
results.push({
error,
result: result as K,
Expand Down

0 comments on commit f425a44

Please sign in to comment.