Menu

gitpiper

repeatGenerator javascript Code Snippet in 2024

functiongeneratoradvanced

Last Updated: 19 September 2024

Creates a generator, repeating the given value indefinitely.

  • Use a non-terminating while loop, that will yield a value every time Generator.prototype.next() is called.
  • Use the return value of the yield statement to update the returned value if the passed value is not undefined.
const repeatGenerator = function* (val) { let v = val; while (true) { let newV = yield v; if (newV !== undefined) v = newV; } };
const repeater = repeatGenerator(5); repeater.next(); // { value: 5, done: false } repeater.next(); // { value: 5, done: false } repeater.next(4); // { value: 4, done: false } repeater.next(); // { value: 4, done: false }

javascript snippet similar to repeatGenerator For You in September 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️