Trying to write a simple script. Described in server.js:

var UserMethods = require('../User'); app.get('/', function (req, res) { var params = UserMethods.userGetParams(req.query.user_id); //Как??? res.render('index', { status: params.status, points: params.points }); }); 

And I do not understand how trite to get data from this function ?? Return asynchronous function in any way. kolbek ok .. but how to use it from the child module in the parent?

Reported as a duplicate by members of Pavel Mayorov , Vadim Ovchinnikov , Grundy javascript Jan 24 '17 at 10:28 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Pay attention to methods 1 and 2 on the link above. - Pavel Mayorov
  • PS How many times do you have to give a link to the answer so that you can finally read it? You have already asked a question about asynchronous problems! - Pavel Mayorov
  • @PavelMayorov, and how to apply them? where to return kolbek? - zoinx2012
  • Please read that answer carefully. Callback does not need to be returned ... - Pavel Mayorov
  • @PavelMayorov, that is, I correctly understand the meaning of the callback in that the function will be executed only after it receives the result of another function (where the callback is specified) in its argument? - zoinx2012

1 answer 1

There are many articles on this eternal question, here is an example from the article " Understanding callback-functions (callbacks) " in Habré:

 // определяем нашу функцию с аргументом callback function some_function(arg1, arg2, callback) { // переменная, генерирующая случайное число в интервале между arg1 и arg2 var my_number = Math.ceil(Math.random() * (arg1 - arg2) + arg2); // теперь всё готово и мы вызываем callback, куда передаём наш результат callback(my_number); } // вызываем функцию some_function(5, 15, function (num) { // эта анонимная функция выполнится после вызова callback-функции console.log("callback called! " + num); });