Answer the question
In order to leave comments, you need to log in
How to make Node JS Execute code in order?
Good time of the day. Help solve the current problem. There is a code like this.
rez;
connection.query('SELECT Некий запрос ', function(err, rows, fields) {
if (err) throw err;
for (var i=0; i < rows.length; i++) {
rez = 'Записываем значение запроса';
connection.query("SELECT ", function(err_photo, rows_photo, fields) {
if (err_photo) throw err_photo;
for(var k=0; k < rows_photo.length; k++){
rez +='Записываем значение 2 запроса';
console.log('1');
};
});
console.log('2');
};
});
Answer the question
In order to leave comments, you need to log in
Wrapping in https://github.com/caolan/async
async.series([
function(){ /* выполнится первым */ },
function(){ /* выполнится вторым */ }
]);
In support of Timur Shemsedinov 's advice , here's the code for you:
var async = require('async');
var queries = ['query_1', 'query_2'],
result = '';
async.reduce(
queries,
result,
function iterator(result, query, callback) {
connection.query("SELECT " + query, function(err_photo, rows_photo, fields) {
if (err_photo) callback(err_photo);
for (var k=0; k < rows_photo.length; k++) {
result += 'Записываем значение 2 запроса';
console.log('1');
}
callback(null, result);
})
},
function done(error, result) {
console.log(error, result);
}
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question