-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): add parallel promiseFinally timeout
- Loading branch information
1 parent
eb993f1
commit 1f7b88b
Showing
5 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function parallel(tasks: any[], fn: (...args: any[]) => any) { | ||
return Promise.all(tasks.map(fn)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isFn } from '../is/isFn' | ||
|
||
export async function promiseFinally( | ||
fn: Promise<any> | Function, | ||
finalFn: Function, | ||
) { | ||
let result | ||
try { | ||
result = await (isFn(fn) ? fn() : fn) | ||
} | ||
finally { | ||
finalFn() | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { promiseFinally } from './promiseFinally' | ||
|
||
export const timeout = function timeout(fn: Function, ms: number, msg: string) { | ||
let timerId: NodeJS.Timeout | ||
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId)) | ||
const timerPromise = new Promise((resolve, reject) => { | ||
timerId = setTimeout(() => reject(new Error(msg)), ms) | ||
}) | ||
return Promise.race([warpPromise, timerPromise]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters