Throwing StopIteration in array extras to stop the iteration

Jason Orendorff jason.orendorff at gmail.com
Tue Mar 5 09:32:25 PST 2013


On Tue, Mar 5, 2013 at 5:42 AM, David Bruant <bruant.d at gmail.com> wrote:

> Currently, if one wants to do stop an iteration early, he/she has to be
> done one of the following way:
> 1)
>     try{
>
>         [2, 8, 7].forEach(function(e){
>                 if(e === 8)
>                     throw "whatever";
>                 console.log(e)
>         }
>     }
>     catch(e){
>         // do nothing, I just want to catch the error in case the iteration
>         // stopped before traversing all elements
>     }
>

Well... here's what I would do.

    for (var e of [2, 8, 7]) {
        if (e === 8)
            break;   // exiting JS loops since 1994
        console.log(e);
    }

Why not use statements for your procedural code?

-j
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130305/39af306d/attachment-0001.html>


More information about the es-discuss mailing list