Answer the question
In order to leave comments, you need to log in
How to make a sequence of loop iterations with asynchronous code?
Good afternoon.
I'm learning node.js, after synchronous php it's a bit difficult but very interesting.
There is this simplified code example:
db.query(" SELECT * FROM users LIMIT 0,100 ", function(err, result) {
for( row of result ) {
request('https://site.ru/api/', function (error, response, body) {
db.query(" UPDATE users SET some_field='"+body+"' WHERE id='"+row['id']+"' ", function(err, result) {
console.log('User updated');
});
});
}
});
Answer the question
In order to leave comments, you need to log in
db.query(sql, async function(err, result) {
for( let row of result ) {
let promise = new Promise((resolve, reject) => {
request(url, function (error, response, body) {
db.query(sql, function(err, result) {
console.log('User updated');
resolve();
});
});
});
let result = await promise;
}
});
There is a lot written about it here:
How to work with async/await in JavaScript loops
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question