Problems to create a callback

1

Hi, I am doing a test on the use of asynchronous functions and we are seeing Callback, they give me the following statement:

  

Implement the "callback" function to receive a number and what   print by console. Use that function to iterate over the array   using the forEach method that they have.

and they give me the following code:

var miArray = [1, 2, 3, 4];

function callback(/*...*/) {
}

miArray./*...*/

I have tried to give you a solution in many ways but I have not been able to, I hope I can receive help, as they demand a particular way.

    
asked by Carlos Cobo 07.12.2018 в 17:43
source

1 answer

0

It only asks you to complete the callback function and the forEach, which is a function of the arrays.

var miArray = [1, 2, 3, 4];

//Esta funcion recive un parametro, en nuestro caso un numero.
function callback(a) {
  //Lo imprimimos en pantalla.
  console.log( a);
}
//Con el forEach, en este caso no es necesario definir los parametros.
miArray.forEach(callback);

// Esto tambien se puede hacer

miArray.forEach( x => { callback(x) });

//Tambien puedes definir el callback dentro del for each.

miArray.forEach( x =>{ console.log(x) });

In this line miArray.forEach(callback); it is not necessary to call the parameters since the forEach already delivers one by default, so internally complete the function: callback(x) .

    
answered by 07.12.2018 / 18:03
source