Posted about 6 years ago. Visible to the public.

Looping through iterators in Coffeescript

Some modern Javascript APIs return iterators Archive instead of arrays.

In plain Javascript you can loop through an iterator using
for...of Archive :

Copy
var iterator = ...; for (var value of iterator) { console.log(value); }

While there is a for...of construct in Coffeescript, it iterates through property/value pairs Archive and cannot be used to access iterable objects.

To iterate through an iterator in Coffeescript you need to do something like this:

Copy
iterator = ...; while (entry = iterator.next()) && !entry.done console.log(entry.value)

The parentheses in the while cannot be omitted.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 4 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more