Jersson G Arrivasplata Rojas
TipLenguajesJavaScript1 min de lectura

Función generadora (ES6)

Validación de llamadas máximas a una función

Validación de llamadas máximas a una función.

javascript
function* clickToMax(max) {
  let index = 0;
  while (index < max) {
    yield index;
    index++;
  }
  return index;
}

Función que valida a la función generadora.

javascript
const max = 2;
const iterator = clickToMax(max);

function validator(iterator) {
  const data = iterator.next();
  if (data.done) data.value = max;
  return data;
}

validator(...) tiene como parámetro la función generadora.

javascript
validator(iterator); // {value: 0, done: false}
validator(iterator); // {value: 1, done: false}
validator(iterator); // {value: 2, done: true}
validator(iterator); // {value: 2, done: true}
Jersson G. Arrivasplata RojasSoftware Engineer & Technical Leader · Lima, Perú
TipLanguagesJavaScript1 min read

Generator function (ES6)

Limiting the maximum number of calls to a function

Limiting the maximum number of calls to a function.

javascript
function* clickToMax(max) {
  let index = 0;
  while (index < max) {
    yield index;
    index++;
  }
  return index;
}

A function that validates the generator function.

javascript
const max = 2;
const iterator = clickToMax(max);

function validator(iterator) {
  const data = iterator.next();
  if (data.done) data.value = max;
  return data;
}

validator(...) takes the generator function as its parameter.

javascript
validator(iterator); // {value: 0, done: false}
validator(iterator); // {value: 1, done: false}
validator(iterator); // {value: 2, done: true}
validator(iterator); // {value: 2, done: true}
Original post
Jersson G. Arrivasplata RojasSoftware Engineer & Technical Leader · Lima, Perú
TipLinguagensJavaScript1 min de leitura

Função geradora (ES6)

Validação do número máximo de chamadas a uma função

Validação do número máximo de chamadas a uma função.

javascript
function* clickToMax(max) {
  let index = 0;
  while (index < max) {
    yield index;
    index++;
  }
  return index;
}

Função que valida a função geradora.

javascript
const max = 2;
const iterator = clickToMax(max);

function validator(iterator) {
  const data = iterator.next();
  if (data.done) data.value = max;
  return data;
}

validator(...) recebe a função geradora como parâmetro.

javascript
validator(iterator); // {value: 0, done: false}
validator(iterator); // {value: 1, done: false}
validator(iterator); // {value: 2, done: true}
validator(iterator); // {value: 2, done: true}
Jersson G. Arrivasplata RojasSoftware Engineer & Technical Leader · Lima, Perú

Apoya mis proyectos

Si una librería, guía o recurso de este sitio te fue útil, puedes aportar con Yape o Plin. Ayuda a mantener el código abierto y la documentación.

987 347 691Jersson G. Arrivasplata Rojas · Soles