Search code examples
javascriptparse.js

Javascript Return Value without breaking loop


My code is currently as follows. I'm using the Parse.Js library.

while (i < workout) {

    return q.find({
        success: function (type) {
            if (type == 3) {
                typeCount++;
            }
        }
    });
    i++;
}

The problem is the 'return' seems to stop the rest of the function from working. Is there a way to do this without breaking the loop?

I tried removing the return all together but then the q.find code doesn't run.


Solution

  • No. return ends execution of the function, breaking all loops, if blocks, etc. You can store the value in a variable and return that at the end (once your loops are finished), but return stops the function.