Reputation: 878
I currently have this situation...
function loop (num1, callback) {
for (i = 0; i < num1; i++) {
console.log("loop count: " + i);
callback();
}
}
Which is currently outputting the following sample results:
loop count: 1
loop count: 2
loop count: 3
...
loop count: num1 - 1
callback output 1
callback output 2
callback output 3
...
callback output num1 - 1
What I would like it to do is wait until each callback has finished executing and outputting its result before moving onto the next item in the loop. i.e. as per the sample output below:
loop count: 1
callback output 1
loop count: 2
callback output 2
loop count: 3
callback output 3
...
loop count: num1 - 1
callback output num1 - 1
Does anyone know how I can achieve this sequential / synchronous mode of operation in my function?
Thanks in advance for the help!
UPDATE...
Ok so the call back function is being called like so...
loop(10, updateDB);
And the callback function passed to loop is as follows...
function updateDB(sql_string, responseObject) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'database',
});
connection.connect();
var query = sql_string;
connection.query(query, function(err, results) {
connection.end();
if (err) {
console.log(err);
responseObject.push({"queryStatus":"FAIL"});
}
if (err == null) {
console.log('Changed ' + results.changedRows + ' rows');
responseObject.push({"queryStatus":"PASS"});
}
});
};
Upvotes: 0
Views: 1303
Reputation: 7273
Your callback is obviously asynchronous, but that doesn't void your desire to force it to execute in its entirety before moving on in your loop. Here's a way by using Promises to call the next loop cycle.
function loop(max, callback, i) {
i = i || 0;
if (i < max) {
console.log("loop count: " + i);
return callback(i).then(function() {
return loop(max, callback, ++i);
});
}
}
function doAsyncStuff(passedNum) {
return new Promise(function(resolve, reject) {
// Fake asynchronous stuff for 500ms using setTimeout.
// Put your real async code here, calling "resolve" when
// it's finished.
setTimeout(function() {
console.log("callback count: " + passedNum);
resolve();
}, 500);
});
}
loop(5, doAsyncStuff).then(function() {
console.log('All done looping!')
});
Upvotes: 4
Reputation: 107
You can use this using promise of javascript
function callback(item) {
return new Promise(function(resolve, reject) {
console.log('callback item ', item);
resolve(item);
});
}
function loopCall(num) {
for (var i = 1; i < num; i++) {
console.log('loop item ', i);
callback(i).then(function(data) {
}, function(err) {
console.log(err);
console.log('Something Broke');
})
}
}
loopCall(120);
This is the way you can get your desired output.If you want use node.js environment and there has a npm module async.
I hope it will be helpfull.
Upvotes: 0