Callbacks explanation of how it works

3

Hello, I'm new to this and I'm not entirely clear on how this code works, first print 'start', then 'finish' and then the callback since it is in an asynchronous subprocess, right? but I do not understand the syntax of

suma(5,6,function(resultadocb){
    console.log(resultadocb); 
}); 

and because it is above callback(resultado);

Could someone tell me step by step the operation of the code please? Thanks

function suma(numero1, numero2, callback){
        var resultado = numero1 + numero2;
        callback(resultado);
}

console.log('empieza'); 

//ESTO ESTA EN UN SUBPROCESO
suma(5,6,function(resultadocb){
    console.log(resultadocb); 
});

console.log('acaba');
    
asked by francisco dwq 09.04.2018 в 14:47
source

2 answers

1

Callbacks are not a subprocess, in fact, they even run asynchronously. The idea of callbacks is to be able to execute an algorithm once a certain action is completed.

Callbacks are functions that are sent as parameters and you can send references to them. This is because the functions are treated as objects or better said First Class Citizens.

When you send the callback to the add function:

suma(1,2, function(){
 //..
});

You are actually sending the reference of an anomin function, not creating a subprocess. So this is also possible:

function obtenerResultado(resultadocb)
{
  console.log(resultadocb); 
}

sumar(1,2, obtenerResultado);

In this case, the function obtenerResultado will be executed.

In summary, the reason why you print everything in order is because the callbacks are not threads or threads, they are function references.

    
answered by 09.04.2018 в 15:42
0
function suma(numero1, numero2, callback){
        var resultado = numero1 + numero2;
        /*->*/callback(resultado);
}

console.log('empieza'); 

//ESTO ESTA EN UN SUBPROCESO
suma(5,6,function(resultadocb){
    console.log(resultadocb); 
});

console.log('acaba');

/ * At the moment of declaring the sum function, it receives 2 values that will be replaced by the user and a third party that will invoke an anonymous function at the same time

As you can notice, the sum function executes the callback to print the sum of both numbers; however, that block of code will not be executed even though the function is only declared but has not yet been invoked

later a message is printed by console

Now if the sum function is called and the 3 values are replaced; in this order:

1.- number1 = 5
2.- number2 = 6
3.- callback is replaced by an anopnimal function that receives a result, this being able to be any other value

When resultcb is printed, the line that makes the sum of both numbers is executed and that higher is indicated by an arrow

Once this function is executed and the result is returned, the last console.log is executed, which says

WHY IT IS EXECUTED IN THAT ORDER

  

Well the INICIA console.log does not depend on anyone because of that it appears   first, although the function is declared at the beginning, it is invoked   then for that and the last console.log to not be dependent on anyone   wait for the cascade reading to show

    
answered by 09.04.2018 в 15:41