11import B from 'bluebird' ;
22import _ from 'lodash' ;
3- import type { LongSleepOptions , WaitForConditionOptions } from './types.js' ;
3+ import type {
4+ LongSleepOptions ,
5+ WaitForConditionOptions ,
6+ } from './types.js' ;
47
58const LONG_SLEEP_THRESHOLD = 5000 ; // anything over 5000ms will turn into a spin
69
710/**
811 * An async/await version of setTimeout
12+ * @param ms - The number of milliseconds to wait
913 */
1014export async function sleep ( ms : number ) : Promise < void > {
1115 return await B . delay ( ms ) ;
@@ -20,10 +24,16 @@ export async function sleep(ms: number): Promise<void> {
2024 * receives an object with the properties `elapsedMs`, `timeLeft`, and
2125 * `progress`. This will be called on every wait interval so you can do your
2226 * wait logging or whatever.
27+ * @param ms - The number of milliseconds to wait
28+ * @param options - Options for controlling the long sleep behavior
2329 */
2430export async function longSleep (
2531 ms : number ,
26- { thresholdMs = LONG_SLEEP_THRESHOLD , intervalMs = 1000 , progressCb = null } : LongSleepOptions = { } ,
32+ {
33+ thresholdMs = LONG_SLEEP_THRESHOLD ,
34+ intervalMs = 1000 ,
35+ progressCb = null ,
36+ } : LongSleepOptions = { } ,
2737) : Promise < void > {
2838 if ( ms < thresholdMs ) {
2939 return await sleep ( ms ) ;
@@ -45,6 +55,9 @@ export async function longSleep(
4555
4656/**
4757 * An async/await way of running a method until it doesn't throw an error
58+ * @param times - The maximum number of times to retry the function
59+ * @param fn - The async function to retry
60+ * @param args - Arguments to pass to the function
4861 */
4962export async function retry < T = any > (
5063 times : number ,
@@ -71,6 +84,10 @@ export async function retry<T = any>(
7184/**
7285 * You can also use `retryInterval` to add a sleep in between retries. This can
7386 * be useful if you want to throttle how fast we retry.
87+ * @param times - The maximum number of times to retry the function
88+ * @param sleepMs - The number of milliseconds to wait between retries
89+ * @param fn - The async function to retry
90+ * @param args - Arguments to pass to the function
7491 */
7592export async function retryInterval < T = any > (
7693 times : number ,
@@ -101,6 +118,8 @@ export const parallel = B.all;
101118/**
102119 * Export async functions (Promises) and import this with your ES5 code to use
103120 * it with Node.
121+ * @param promisey - A Promise or promise-like value to convert to a callback
122+ * @param cb - The callback function to call with the result or error
104123 */
105124// eslint-disable-next-line promise/prefer-await-to-callbacks
106125export function nodeify < R = any > ( promisey : any , cb : ( err : any , value ?: R ) => void ) : Promise < R > {
@@ -109,6 +128,7 @@ export function nodeify<R = any>(promisey: any, cb: (err: any, value?: R) => voi
109128
110129/**
111130 * Node-ify an entire object of `Promise`-returning functions
131+ * @param promiseyMap - An object containing functions that return Promises
112132 */
113133export function nodeifyAll < T extends Record < string , ( ...args : any [ ] ) => any > > (
114134 promiseyMap : T ,
@@ -126,13 +146,18 @@ export function nodeifyAll<T extends Record<string, (...args: any[]) => any>>(
126146
127147/**
128148 * Fire and forget async function execution
149+ * @param fn - The function to execute asynchronously
150+ * @param args - Arguments to pass to the function
129151 */
130152export function asyncify ( fn : ( ...args : any [ ] ) => any | Promise < any > , ...args : any [ ] ) : void {
131153 B . resolve ( fn ( ...args ) ) . done ( ) ;
132154}
133155
134156/**
135157 * Similar to `Array.prototype.map`; runs in serial or parallel
158+ * @param coll - The collection to map over
159+ * @param mapper - The function to apply to each element
160+ * @param runInParallel - Whether to run operations in parallel (default: true)
136161 */
137162export async function asyncmap < T , R > (
138163 coll : T [ ] ,
@@ -152,6 +177,9 @@ export async function asyncmap<T, R>(
152177
153178/**
154179 * Similar to `Array.prototype.filter`
180+ * @param coll - The collection to filter
181+ * @param filter - The function to test each element
182+ * @param runInParallel - Whether to run operations in parallel (default: true)
155183 */
156184export async function asyncfilter < T > (
157185 coll : T [ ] ,
@@ -188,6 +216,8 @@ export async function asyncfilter<T>(
188216 * error then this exception will be immediately passed through.
189217 *
190218 * The default options are: `{ waitMs: 5000, intervalMs: 500 }`
219+ * @param condFn - The condition function to evaluate
220+ * @param options - Options for controlling the wait behavior
191221 */
192222export async function waitForCondition < T > (
193223 condFn : ( ) => Promise < T > | T ,
@@ -215,11 +245,18 @@ export async function waitForCondition<T>(
215245 return await spin ( ) ;
216246 }
217247 // if there is an error option, it is either a string message or an error itself
218- throw error
219- ? _ . isString ( error )
220- ? new Error ( error )
221- : error
222- : new Error ( `Condition unmet after ${ waited } ms. Timing out.` ) ;
248+ if ( error ) {
249+ throw _ . isString ( error ) ? new Error ( error ) : error ;
250+ }
251+ throw new Error ( `Condition unmet after ${ waited } ms. Timing out.` ) ;
223252 } ;
224253 return await spin ( ) ;
225254}
255+
256+ // Re-export types
257+ export type {
258+ Progress ,
259+ ProgressCallback ,
260+ LongSleepOptions ,
261+ WaitForConditionOptions ,
262+ } from './types.js' ;
0 commit comments