2
something.some((test) => {
    if(a == 0) {
        if(b == 0) {
            continue;
        }
    }
});

Can I skip one loop with this continue?

The WebStorm reports continue outside of loop statement.

John Doe
  • 21
  • 1
  • 2
    `continue` is only for traditional `for` and `while` loops. – Pointy Aug 27 '19 at 15:16
  • 1
    No, you cannot. `some` is not a loop construct, but a method. – VLAZ Aug 27 '19 at 15:16
  • Possible duplicate of [Short circuit Array.forEach like calling break](https://stackoverflow.com/questions/2641347/short-circuit-array-foreach-like-calling-break) – VLAZ Aug 27 '19 at 15:17

1 Answers1

6

You can't use continue, but you can use return to skip the rest of the statements in your handler and continue with the next item:

something.some((test) => {
    if(a == 0) {
        if(b == 0) {
            return;
        }
        // X...
    }
    // Y...
});

If the logic hits the return for an entry, then neither the code at X or Y above will be executed for that entry.

If you want the equivalent of break (stop the "loop" entirely) within a some callback, return true.


Side note: That usage of some looks suspect. Normally, you only use some if you return a value from the some callback (telling some whether to continue). Probably 90% (waves hands) of the time you also use some's return value to know whether the loop was stopped early or continued. If you never return anything from the callback, forEach (or a for-of loop) would be the idiomatic choice, not some.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875