Read more

Looping through iterators in Coffeescript

Henning Koch
March 29, 2016Software engineer at makandra GmbH

Some modern Javascript APIs return iterators Show archive.org snapshot instead of arrays.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

In plain Javascript you can loop through an iterator using
for...of Show archive.org snapshot :

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 Show archive.org snapshot and cannot be used to access iterable objects.

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

iterator = ...;

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

The parentheses in the while cannot be omitted.

Posted by Henning Koch to makandra dev (2016-03-29 10:09)