Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear timeout if promise has already resolves to avoid hanging open handle #123

Closed
Closed
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
5 changes: 3 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ const checkUrlFilters = (request: MockedRequest, options: PactMswAdapterOptionsI
};

const addTimeout = async<T>(promise: Promise<T>, label: string, timeout: number) => {
let timeoutId: NodeJS.Timeout
const asyncTimeout = new Promise<void>((_, reject) => {
setTimeout(() => {
timeoutId = setTimeout(() => {
reject(new Error(`[pact-msw-adapter] ${label} timed out after ${timeout}ms`));
}, timeout);
});

return Promise.race([promise, asyncTimeout]);
return Promise.race([promise, asyncTimeout]).then(() => clearTimeout(timeoutId));
}

export { log, logGroup, createWriter, checkUrlFilters, addTimeout };
Loading