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

delay/sleeps/timeouts helper #3267

Open
mfulton26 opened this issue Nov 15, 2024 · 4 comments
Open

delay/sleeps/timeouts helper #3267

mfulton26 opened this issue Nov 15, 2024 · 4 comments
Labels
c: feature Request for new feature has workaround Workaround provided or linked p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Milestone

Comments

@mfulton26
Copy link

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

@mfulton26 mfulton26 added c: feature Request for new feature s: pending triage Pending Triage s: waiting for user interest Waiting for more users interested in this feature labels Nov 15, 2024
Copy link
Contributor

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

View more issues which are waiting for user interest

@ST-DDT ST-DDT added this to the vFuture milestone Nov 15, 2024
@ST-DDT ST-DDT added p: 1-normal Nothing urgent and removed s: pending triage Pending Triage labels Nov 15, 2024
@ST-DDT
Copy link
Member

ST-DDT commented Nov 15, 2024

Interesting idea.

Just a quick question:

  1. Do you need the method to wait

aka

const { timeout } = faker.helpers;
// Do 1
await timeout(waitOptions)
// Do 2
await timeout(waitOptions)
// Do 3
  1. or do you need it to delay an async response:
return faker.helpers.timeout(waitOptions, () => newPerson());
  1. 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>;

@mfulton26
Copy link
Author

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.

@ST-DDT
Copy link
Member

ST-DDT commented Nov 16, 2024

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))
  );
}

@ST-DDT ST-DDT added the has workaround Workaround provided or linked label Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature has workaround Workaround provided or linked p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Projects
None yet
Development

No branches or pull requests

2 participants