Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
create a function that resolves/returns the value of other functions Typescript

I know theres probably an easy answer to this but I'm trying to create a function that resolves / returns the value of a variable amount of functions into an array. However it looks like typescript doesn't like this. What would be the appropriate way to write / fix this function

const func = (x:number, y:number) => {x,y}

const executeRepetitiveFunction = (num: number, func: () => unknown) => {

    const returnValues: Array<(unknown)> = []

    for (let i = 0; i < num; i++) {
        returnValues.push(func())
    }

    return returnValues
}

error

var result = func();
                     ^

TypeError: func is not a function
    at executeRepetitiveFunction

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to return something from the function you are passing and store it in an array, you can just call it and declare func as a Function type. You can do something like this:

const executeRepetitiveFunction = (num: number, func: Function) => {

  const returnValues: Array<(unknown)> = []

  for (let i = 0; i < num; i++) {
      returnValues.push(func())
  }

  return returnValues
}

const testFunction: Function = (): String => {
    return 'ok';
}

executeRepetitiveFunction(5, testFunction); 

This will return ok 5 times and store it in an array . // Output: ['ok', 'ok', 'ok', 'ok', 'ok']

about 4 years ago · Juan Pablo Isaza Denunciar

0

As someone said, your code doesn't make sense. The error you're seeing is completely accurate - you have no function defined called func. You have func set up as the name of a parameter in your executeRepetitiveFunction function declaration, but it's not available outside of that function's scope.

So that error is irrelevant to the question you're asking, and I'll just answer the question you're actually asking.

const executeRepetitiveFunction = (num: number, func: () => unknown) : Promise<Array<any>> => {

    return new Promise((resolve, reject) => {
        const returnValues: Array<any> = []
    
        for (let i = 0; i < num; i++) {
            returnValues.push(func())
        }
    
        resolve(returnValues);
    });
}

const valuesPromise = executeRepetitiveFunction(5, () => {
    return "Execution complete"
});

So what happens here? First we declare our executeRepetitiveFunction function, which takes two parameters, a number and a function which returns an unknown data type. Then we return a promise from this function, which resolves once the loop is complete.

We execute the function after it's declared, by calling the executeRepetitiveFunction function and passing it a number (5) and an anonymous function that simply returns a value.

Then when that's done, we can access our values by calling valuePromise.then(valuesArray => {console.log(valuesArray}); or handling it however else you want.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda