Asynchronous passing a result to callback in JavaScript

Now let an asynchronous operation, after its completion, not output anything to the console, but receive a certain result. Let it be an array with data, which, for example, could be received via AJAX. But since we don’t know how to work with AJAX yet, we’ll just simulate this receiving:

function make() { setTimeout(function() { let res = [1, 2, 3, 4, 5]; // an array with result }, 3000); }

Let's make it so that the array with the result is passed to the callback parameter:

function make(callback) { setTimeout(function() { let res = [1, 2, 3, 4, 5]; callback(res); // passing the result as a parameter }, 3000); }

Now, when passing the callback to the make function call, we can write a parameter in it - and the result of the asynchronous operation will fall into this parameter:

make(function(res) { console.log(res); // our array });

Add the callback code so that it finds the sum of the array elements with the result.

enru