I have 3 things I need to carry out but they have to be in order. I also tried the async library but still could not get it to work.
1) Get data for the db (I just used an array for demonstration purposes).
2) Run a for loop
3) if the "if" condition in the for loop does not run then execute my second callback i have below the for loop.
It is the second function in the waterfall that I can't quite solve. How can I make sure the for loop executes first then choose which callback to execute based on the condition?
var async = require('async');
async.waterfall([
function(callback) {
//some db query to fetch data --using fake data instead
var fakeData =[1,2,3,4,5,6,7,8,9];
callback(null, fakeData);
},
function(fakeData, callback) {
//this for loop has to run first!
for(var i = 0; i < fakeData.length; i++){
if(fakeData[i] == '5'){
return callback(null, '5');
}
}
//this block of code can only run after the for loop has finished
callback(null, 'none');
},
function(str, callback) {
if(str == '5'){
callback(null, 'exist');
} else {
callback(null, 'not');
}
}
], function (err, result) {
console.log(result);
});
returning inside the if statement in the loop would fix that.callbackuntil the loop has completed.