Is there an equivalent statement to 'continue' when using node.js async forEachSeries?
Asked Answered
E

1

6

I am using the node.js async package, specifically forEachSeries, to make a series of http requests based on parameters drawn from an array. In the callback of each request I have some if/else statements to respond to different types of responses.

// This is the callback of a GET request inside of a forEachSeries
function(error, response) {
    if (response.results) {
        // Do something with results
    }
    else if (!response.results) {
        // Would like to use a continue statement here, but
        // this is not inside of a loop
    }
    else {
        // Do something else
    }
}

Is there an equivalent to 'continue' that I can use inside of the else if above? This is not technically inside of a loop so continue does not work.

Edging answered 11/4, 2012 at 13:15 Comment(2)
There is a reason for you to don't have access to the continue statement inside a control structure like if/else. What exactly are you trying to do? 'Cause it looks to me like you need to review your logic...Calhoun
The !response.results is most likely due to some rate limiting from the server. Currently, I can return the callback() on the forEachSeries that the request above is inside, but there is some potential data loss with this approach. Just trying to figure out if there is an equivalent to continue in a forEachSeries in node.js. Something similar to how return true is used in a jQuery $each.Edging
A
6

Since it is just a function you should be able to return from it to have the same effect:

else if (!response.results) {
    return;
}
Accidental answered 11/4, 2012 at 13:21 Comment(4)
This is what I was thinking, though the above code is a little weird -- the else statement will never be hit, and in this case, a simple if/else would suffice and would not likely need a continue type of flow control.Cementite
@Cementite - Agreed, although it could make more sense in a larger code snippet, for example if there was another branch of code within the else if.Accidental
@Justin - Thanks for the response. This didn't completely fix my issue, but it is the simplest solution for the question so I accepted. I should have posted a larger snippet.Edging
@ Reid - Thanks for the input. Yes it does appear that the else will never hit, but there was another case that was neither response.results or !response.results. My shortcoming on the small snippet.Edging

© 2022 - 2024 — McMap. All rights reserved.