L
L
learn0012016-04-25 19:20:36
Node.js
learn001, 2016-04-25 19:20:36

How do callbacks work?

newbie. I smoke an example. I can't figure out how the callbacks work.
in the model (left). what I understand:
query(sql, there is a json response with data and an error). the rest is magic for me. please help me figure it out.
237e5c1b486743c080dd3b980e421675.png
on the right in the controller when requesting the root, magic, then we say where we draw something from our object.
can someone explain the magic of callbacks? I will be very)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemme, 2016-04-25
@learn001

Mb on this example will be easier.

// асинхронная функция, которое аргументом передаем callback
function asyncFunction(callback) {
  setTimeout(function() {
  	callback('Hello, World!'); // вызываем callback и передаем ему ответ 'Hello, World'
  }, 2000);
}

// Вызываем асинхронную функцию, но чтоб получить ответ, нужно передать callback
// Т.е передаем другую функцию
asyncFunction(function(response) {
  alert(response); // выводим ответ.
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question