Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './communication';
export * from './dateTime';
export * from './geo';
export * from './arrays';
export * from './utils';
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './timeout';
15 changes: 15 additions & 0 deletions src/utils/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
export async function timeout(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export class TimeoutError extends Error {}

export const racePromiseWithTimeout = async <T>(msTimeout: number, promise: Promise<T>): Promise<T> => {
// Create a promise that rejects in <ms> milliseconds
const timeout = new Promise<T>((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(new TimeoutError(`Timed out in + ${msTimeout} + ms.`));
}, msTimeout);
});

// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]);
};
2 changes: 1 addition & 1 deletion tests/configurations/unit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 66,
functions: 94,
functions: 95,
lines: 89,
statements: 90,
},
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/utils/timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TimeoutError, racePromiseWithTimeout } from './../../../src/utils';

describe('raceTimeout', () => {
it('should resolve with the promise value if it resolves before the timeout', async () => {
const value = 'test value';
const promise = Promise.resolve(value);
const result = await racePromiseWithTimeout(1000, promise);
expect(result).toBe(value);
});

it("should reject with the promise's rejection reason if it rejects before the timeout", async () => {
const error = new Error('Test error');
const promise = Promise.reject(error);
await expect(racePromiseWithTimeout(1000, promise)).rejects.toBe(error);
});

it('should reject with a TimeoutError if the promise takes longer than the timeout', async () => {
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve('success');
}, 300);
});

await expect(racePromiseWithTimeout(200, promise)).rejects.toThrow(TimeoutError);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2022",
"allowSyntheticDefaultImports": true,
"strict": true,
"module": "commonjs",
Expand Down