Throwing StopIteration in array extras to stop the iteration

Andrea Giammarchi andrea.giammarchi at
Tue Mar 5 10:10:48 PST 2013

love the token/Halt idea +1 easy to polyfill too

However, and this is for David, some and every are *not* the opposite. With
some, you return true only when you want to exit, with every you have to
return true or false accordingly.

So, the return true in Array#some() is the equivalent of a break, while no
return false is necessary.

This is not the case of every, where the first time you don't return true,
you are out.

You know that but I just wonder how come you think these methods are
confusing, I think these are OK.


On Tue, Mar 5, 2013 at 10:00 AM, Tab Atkins Jr. <jackalmage at>wrote:

> On Tue, Mar 5, 2013 at 9:38 AM, Kevin Gadd <kevin.gadd at> wrote:
> > Maybe I'm overlooking some past discussion on this, but if you want to
> > be able to generally terminate iteration in constructs like
> > Array.forEach (this seems like a pretty real world use-case), why not
> > introduce an additional argument to the forEach/etc callbacks, for an
> > 'iteration token'?
> >
> > I.e. if forEach currently passes 'item, index, array' to the callback,
> > it can pass 'item, index, array, token'. token can be an object, such
> > that:
> >
> > token.stopIteration(); // probably a better name for this
> >
> > terminates iteration.
> >
> > This has some upsides:
> > You can mechanically detect a conforming implementation by looking for
> > the token object via arguments[3]
> > There's no risk of code written for a conforming implementation
> > causing weird side effects (like an uncaught StopIteration) in a
> > non-conforming iteration; it'll fail meaningfully
> > The token is conceptually easy to understand and the implementation cost
> is low
> Additional upside: we can canonically call the argument "halt", thus
> finally completing Brendan's name in forEach's signature:
> forEach(Element, Index, Collection, Halt).  ^_^
> ~TJ
> _______________________________________________
> es-discuss mailing list
> es-discuss at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list