admin管理员组

文章数量:1401628

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.

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.

Share Improve this question edited Feb 11, 2013 at 15:28 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Apr 11, 2012 at 13:15 TankofVinesTankofVines 1,1872 gold badges14 silver badges23 bronze badges 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... – Daniel Ribeiro Commented Apr 11, 2012 at 13:18
  • 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. – TankofVines Commented Apr 11, 2012 at 13:29
Add a ment  | 

1 Answer 1

Reset to default 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;
}

本文标签: