Iterators and generators in ECMAScript 6
[2015-02-26] New version of this blog post: “ Iterables and iterators in ECMAScript 6 ” The iterator pattern enables unified and simple access to the elements stored in data structures. Generators are lightweight coroutines . Think: functions that can be suspended and resumed. Among other things, they help with implementing iterators. This blog post explains how iterators and generators work in ECMAScript 6. The iterator protocol has recently changed, this post explains the new protocol. Iterators In the iterator pattern, the iterator is an object that functions as a “pointer” into a data structure. It produces a sequence with all of the data structure’s elements. The pattern has two advantages: First, you don’t need an intermediate data structure for storing the sequence of elements. Thus, it is reminiscent of explicit lazy programming and allows you to work with large (e.g. disk-based) data structures. Second, many algorithms (map, filter, etc.) can based on iterators and are dec...