last value from iterator
Axel Rauschmayer
axel at rauschma.de
Sun Apr 5 23:13:01 UTC 2015
The only iterating mechanism that lets you access the “`done` value” is `yield*`. All other constructs (`for-of`, spread, destructuring, …) completely ignore it. Its main purpose is to let `yield*` make recursive generator calls with results, without disrupting normal yielding. That is, for normal iteration it is an out-of-band value.
Axel
> On 06 Apr 2015, at 00:43, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:
>
> Thanks, that helps. I had been thinking that for-of was a model for how iterators should be consumed.
>
> When implementing any kind of iterator (generator or not), it seems important to recognize that if you choose to return a value with done set to true, you are precluding the use of for-of with that iterator (assuming you want all the values).
>
> On Sun, Apr 5, 2015 at 11:40 AM, Allen Wirfs-Brock <allen at wirfs-brock.com <mailto:allen at wirfs-brock.com>> wrote:
>
> On Apr 5, 2015, at 6:04 AM, Mark Volkmann wrote:
>
>> I thought that when an iterator returns done: true, the value should not be used.
>
> Why do you think that? The definition of the IteratorResult interface in the the ES6 spec. doesn't say that.
>
>> However, if a generator function ends by returning a value, done will be true when that value is returned and the value should be used.
>
> Could be used. Whether a generator or any Iterator provides a meaningful value when it reach the the 'done' state depends upon its specific definition.
>
>> Given this, how can a consumer know the correct way to handle the value when done is true? Clearly consumers shouldn't have to be aware of whether the iterator is actually a generator and whether it ends by returning a value.
>
> An Iterator does not have to be a generator to provide a 'done' state value. In general, If you are going to do anything other than basic iteration, such as is performed by for-of, you need to know about the specific Iterator you are using.
>
> Allen
>
>
>
>
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
--
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150406/d18d2155/attachment-0001.html>
More information about the es-discuss
mailing list