Answer the question
In order to leave comments, you need to log in
How to wait for a function to execute and pass its result to a variable?
There is a code - two modules on js, in the first one, sequential execution and filling the variable with the results of adjax requests are written on promises. In the second, I use this function and want to perform one or another action depending on the result. It also failed to write on promises. I ask for your help.
var module1 = (function(){
console.log('Module module1 is initiating');
function _sendsql2server(data){
promise = $.when();
data.sql.forEach(function (v,k){
promise = promise.then(function(){
return $.ajax({
// код удален
});
}).then(function(thenResult){
console.log('Запрос №'+k+' - выполнен');
data.response.push({"data":thenResult.data,"msg":"Ok"});
}, function(jqXHR, exception){
console.log('Ошибка');
return $.Deferred();
});
});
promise.then(function(){
return data;
console.log('Все ok');
});
}
return {_sendsql2server: _sendsql2server};
})();
var checkTableModule = (function (module1) {
$('form').on('submit', function(){
// вырезал код
var _sql= '...'; // Формирую переменую и отправляю ее в функцию
var result = module1._sendsql2server(_sql);
console.log(result);
if(result != ''){
console.log('Данные присутствуют');
somefunction1(result);
} else {
console.log('Данные отсутствуют');
somefunction2();
}
});
})(module1 || {});
Answer the question
In order to leave comments, you need to log in
You can wait a couple of years:
setTimeout(function(){
console.log('уф, дождались!')
}, 63072000)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question