node js callback inside for loop code example
Example 1: node js callback
function processData (callback) {
fetchData(function (err, data) {
if (err) {
console.log("An error has occurred. Abort everything!");
return callback(err);
}
data += 1;
callback(null, data);
});
}
processData(function (err, data) {
if (err) {
console.err(err);
} else {
console.log("Data: " + data);
}
});
Example 2: javascript iterator callback
for (let i = 0; i < a.length; i++) {
makeHttpReqest("http://www.codegrepper.com", function(response) {
});
}
for (var i = 0; i < a.length; i++) (function(i)
{
makeHttpReqest("http://www.codegrepper.com", function(response) {
});
}) (i);