All files / promise/src resolveAfterTimeout.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                                2x     1x 2x    
/**
 * Creates a {@link Promise} that resolves after `ms` milliseconds with a
 * `value`.
 *
 * @example
 * Resolve after 100ms
 * ```ts
 * resolveAfterTimeout(100)
 *   .then(() => console.log("then"))    // will be called after 100ms
 *   .catch(() => console.log("catch")); // this will never be called
 * ```
 *
 * @example
 * Resolve after 100ms with `"val"`
 * ```ts
 * resolveAfterTimeout(100, "val")
 *   .then((val) => console.log(val))    // will be called after 100ms
 *   .catch(() => console.log("catch")); // this will never be called
 * ```
 *
 * @param ms - the number of milliseconds to wait
 * @param error - the optional value to resolve with
 * @returns a {@link Promise} that resolves after `ms` milliseconds
 */
export const resolveAfterTimeout: {
  (ms: number): Promise<void>;
  <T>(ms: number, value: T): Promise<T>;
} = <T>(ms: number, value?: T): Promise<T | void> => {
  return new Promise((resolve) => setTimeout(resolve, ms, value));
};