Skip to content

Unify all of the sleep helper functions #2331

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

Merged
merged 1 commit into from
Jun 29, 2023
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
10 changes: 3 additions & 7 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
import { ReadablePromise } from './readablepromise';
import { ClientStorage } from './storage';
import { globalSchema, IndexedStructureDefinition, indexSearchParameter, indexStructureDefinition } from './types';
import { arrayBufferToBase64, createReference, ProfileResource } from './utils';
import { arrayBufferToBase64, createReference, ProfileResource, sleep } from './utils';

export const MEDPLUM_VERSION = process.env.MEDPLUM_VERSION ?? '';

Expand Down Expand Up @@ -2452,9 +2452,7 @@ export class MedplumClient extends EventTarget {
} catch (err: any) {
this.retryCatch(retry, maxRetries, err);
}
await new Promise((resolve) => {
setTimeout(resolve, retryDelay);
});
await sleep(retryDelay);
}
return response as Response;
}
Expand All @@ -2479,9 +2477,7 @@ export class MedplumClient extends EventTarget {
}
}
}
await new Promise((resolve) => {
setTimeout(resolve, retryDelay);
});
await sleep(retryDelay);
}
return this.parseResponse(resultResponse as Response, 'POST', statusUrl);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,13 @@ export function findResourceByCode(
: getCodeBySystem(r.code || {}, system) === getCodeBySystem(code, system)
);
}

/**
* Sleeps for the specified number of milliseconds.
* @param ms Time delay in milliseconds
* @returns A promise that resolves after the specified number of milliseconds.
*/
export const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
7 changes: 1 addition & 6 deletions packages/server/src/test.setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createReference, getReferenceString, ProfileResource } from '@medplum/core';
import { createReference, getReferenceString, ProfileResource, sleep } from '@medplum/core';
import {
AccessPolicy,
Bundle,
Expand Down Expand Up @@ -198,8 +198,3 @@ export async function waitForAsyncJob(contentLocation: string, app: Express, acc
}
throw new Error('Async Job did not complete');
}

const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});