The Wayback Machine - https://web.archive.org/web/20250418024906/https://github.com/lodash/lodash/issues/4763
Skip to content

The proposal of add '_.until' function #4763

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

Closed
alperen opened this issue May 9, 2020 · 2 comments
Closed

The proposal of add '_.until' function #4763

alperen opened this issue May 9, 2020 · 2 comments

Comments

@alperen
Copy link

alperen commented May 9, 2020

Sometimes, we would need to call a function more than once to get or generate our expected value. That functions have side-effects that such as changing by the time, API calls or full of randomized values. In this case, we should wrap our function with another function. This wrap functions should call our actual function until return the value on our expecting way. In examples that will you see in the below, we call that reference function as the barrier function.

Sketch

image

Syntax:

_.until(actualFunction, [...arguments], barrierFunction, {retry: Number}?);

Glossary:

  • The actual function is the function that could we want to call it more than once.
  • Arguments: The arguments array exists for the pass to data to our actual function.
  • The barrier function is the reference function to stop the iteration and return the last generated value. That function should return boolean values. If it returns true the iteration will break and ._until the function should return the last value of the actual function.
  • options: There is just one option argument on my mind at the moment. The retry value defines the maximum iteration count of the ._until function. If we can't find the expected value (based to the barrier function) before exceed the retry number, we should the last returned value immediately. This saves us to enter endless loops.

Examples

Example 1

const randomizedNumber = _.until(_.random, [1, 5], (number) => number % 2);

In this example, randomizedNumber variable is absolutely even number. The ._until function will iterate the actual function until the barrier broke.

Example 2

const randomizedNumber = _.until(_.random, [1, 5], (number) => number % 2, {retry: 3});

In this example, randomizedNumber variable could be even or odd number. The ._until function will iterate 3 times maximum then return the last value without test the data with barrier function.

@ncjones
Copy link

ncjones commented Jun 3, 2020

You can use retry-assert. IMO this is the best solution out there for this problem in the Nodejs ecosystem. I've been using it for a few years now with all my Cucumber JS test suites. Disclaimer: I'm the author.

@bnjmnt4n
Copy link
Contributor

This seems like a very specific use case, and there seems to already be a well-defined library for that. Hence, I'm closing this issue for now as we'd prefer to keep this method outside of lodash but we'll keep an eye on the popularity of the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants