0

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);
});

3
  • What are you actually seeing when you run this? I get an error about the callback already being called. If that's what you get, then returning inside the if statement in the loop would fix that. Commented Jul 20, 2016 at 22:50
  • Hi, i added return and the code works. However nodejs is asynchronous so how can I ensure that the for loop will 100% run before the callback function i have after the loop?? Commented Jul 20, 2016 at 22:53
  • for loops are not actually asynchronous, so it's in the language not to call callback until the loop has completed. Commented Jul 20, 2016 at 23:04

1 Answer 1

1

You were getting an error about callback already being called from the async library because you were calling callback if you found '5' in the array, and then again after iterating over the whole array.

Adding the return statement in your if block stops execution of the loop and the function, preventing the second call to callback to be run.

for loops in javascript (regardless of the engine) are synchronous, meaning they will iterate until the condition is false or something is done to change the flow of control (e.g. break or return). Only then will the flow of control leave the loop. Using a return will cause the flow of control to leave the containing function and pass back to the caller, aborting any further loops or any other code in the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.