Loading topic
{ [key]: value }), and how spread and destructuring look in code. It covers the first half of the iteration contract: what makes a value iterable. The next chapter covers the object that5 min readSymbol.iterator method. This chapter covers what that method returns: an iterator, which is the object that actually hands out values one at a time. This chapter assumes you know functions and closures, objects and methods, and 4 min readMap, Set, and spread syntax. It also assumes you have read the previous two chapters, which explain that an iterable has a Symbol.iterator method and that this method returns an iterator with next(). Here we look at the iterables the language gives you f4 min read[Symbol.iterator]() method, and that method returns an iterator whose next() 4 min read[Symbol.iterator]. It also assumes you have read the earlier chapters on the iterable and iterator protocols. In this chapter you write both protocols yourself, with no generators. Doing it by hand show3 min readnext() method that returns { value, done } objects, and that you know which language features consume iterables. Here you will add the third, optional iterator method, return(), and learn exactly when JavaScript calls3 min readnext() method, tracked state in closure variables, and returned { value, done } objects yourself. That approach works, but most of the code is bookkeeping. A generator function is a special kind of function that produces an iterat4 min readfunction* and yield, and that you know the iterator protocol: next() returns { value, done }. You should also be comfortable with closures and try...catch. Here we look at what happens at runtime, step by step. You will learn when t5 min read[Symbol.iterator].5 min readfunction*, yield, lazy start, and how a generator pauses and resumes. It also assumes you have used generators as the Symbol.iterator method of a class. Here you will learn yield*, which lets one generator hand control to an4 min readyield, resumes on next(), and finishes with { done: true }, as covered in the chapter on how generators run. You should also be comfortable with try...catch and with reading { value, done } results by hand.4 min readyield, how return() and throw() resume them from outside, and how for...of calls an iterator's return() when you leave a loop early. Here you put those pieces together. You will see how a generator releases a resource reliably, where th4 min readyield, yield*, and the way for...of calls return() when a loop exits early. All of these were covered in earlier chapters. Here you will use generators to describe sequences that compute values only on request. Some of those 4 min readmap and take. ES2025 adds methods to Iterator.prototype that do the same work, so most of those hand-written helpers are no longer needed. This chapter covers what the helper methods are, how they behave, where th4 min readnext() returns { value, done } right away, and for...of reads one result after another. Many data sources can't work that way because they deliver values over time: network responses, file chunks, database cursors, and message 5 min readyield* delegates, how try...finally cleans up a generator, and how for await...of consumes an async iterable. You also need to be comfortable with promises and async/await.4 min readyield*, return() and try...finally cleanup, lazy pipelines, and async generators. Each section below names a common mistake, explains ho5 min read