-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
delay/sleeps/timeouts helper #3267
Comments
Thank you for your feature proposal. We marked it as "waiting for user interest" for now to gather some feedback from our community:
We would also like to hear about other community members' use cases for the feature to give us a better understanding of their potential implicit or explicit requirements. We will start the implementation based on:
We do this because:
|
Interesting idea. Just a quick question:
aka const { timeout } = faker.helpers;
// Do 1
await timeout(waitOptions)
// Do 2
await timeout(waitOptions)
// Do 3
return faker.helpers.timeout(waitOptions, () => newPerson());
async function timeout(delay: number | { min: number, max: number}): Promise<void>;
async function timeout(delay: number | { min: number, max: number}, fn: () => T): Promise<T>; |
I don't think delaying an async response is needed as this is already pretty straight forward with await timeout(waitOptions);
return newPerson(); I'm not against the idea though. The flexibility does seem nice. Adding a random-ish delay though in the middle of an |
Yeah, putting the method as an argument has some implications that are easier to write yourself, then encoding it in parameters.
Here are my example implementations that can be used while the feature is gathering user interest: /**
* Creates a delay before resolving the promise.
*
* @param ms The maximum number of milliseconds to delay or the millisecond range of delay.
* @param ms.min The minimum number of milliseconds to delay. Defaults to `0`.
* @param ms.max The maximum number of milliseconds to delay.
*
* @returns An awaitable promise that resolves after the delay.
*/
export async function delay(
ms: number | { min: number; max: number }
): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, faker.number.int(ms)));
} Usage: async function mockSomethingNetwork() {
await delay(500);
const entity = mockEntity();
await delay({min: 100, max: 5000 });
entity.children = mockEntityChildren();
return entity;
} If you are interested in the variants with a nested function call: export async function delayReturn<T>(
ms: number | { min: number; max: number },
fn: () => T | Promise<T> = () => undefined as T
): Promise<T> {
const result = fn();
return new Promise((resolve) =>
setTimeout(() => resolve(result), faker.number.int(ms))
);
}
export async function delayInvoke<T>(
ms: number | { min: number; max: number },
fn: () => T | Promise<T> = () => undefined as T
): Promise<T> {
return new Promise((resolve) =>
setTimeout(() => resolve(fn()), faker.number.int(ms))
);
} |
Clear and concise description of the problem
A convenient helper for add random delays/sleeps/timeouts (e.g. to simulate non-deterministic network loads).
Suggested solution
Alternative
Pull in and use delay or roll it myself:
Additional context
No response
The text was updated successfully, but these errors were encountered: