faker
faker copied to clipboard
delay/sleeps/timeouts helper
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
await faker.helpers.timeout({ min: 400, max: 1000 });
Alternative
Pull in and use delay or roll it myself:
await new Promise((resolve) => setTimeout(resolve, faker.number.int({ min: 400, max: 1000 })));
Additional context
No response
Thank you for your feature proposal.
We marked it as "waiting for user interest" for now to gather some feedback from our community:
- If you would like to see this feature be implemented, please react to the description with an up-vote (:+1:).
- If you have a suggestion or want to point out some special cases that need to be considered, please leave a comment, so we are aware about them.
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:
- the number of votes (:+1:) and comments
- the relevance for the ecosystem
- availability of alternatives and workarounds
- and the complexity of the requested feature
We do this because:
- There are plenty of languages/countries out there and we would like to ensure that every method can cover all or almost all of them.
- Every feature we add to faker has "costs" associated to it:
- initial costs: design, implementation, reviews, documentation
- running costs: awareness of the feature itself, more complex module structure, increased bundle size, more work during refactors
Interesting idea.
Just a quick question:
- Do you need the method to wait
aka
const { timeout } = faker.helpers;
// Do 1
await timeout(waitOptions)
// Do 2
await timeout(waitOptions)
// Do 3
- or do you need it to delay an async response:
return faker.helpers.timeout(waitOptions, () => newPerson());
- or both? The API could be flexible to cover both:
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 async & await:
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 async code block was my main motivation.
I don't think delaying an async response is needed as this is already pretty straight forward with
async&await:
Yeah, putting the method as an argument has some implications that are easier to write yourself, then encoding it in parameters.
- When is the given method executed? Before the delay or after or somewhen in between?
- Does the time of the function execution count towards the delay or is it separate?
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))
);
}
I recently needed this feature for simulating network delays. IMO the simple delay method is enough (for now), as every other implementation would require handling both resolve and reject and chaining it with await or then is easy enough.
Is this a helper method or a datatype method?
Or a date method 😀