Implementing the loadYears method with generators

Now let's look at the loadYears method.

The goal of this method is to generate a list of years, which will then be passed to the view for rendering. Those years could be selected as start year and end year in the user interface. They'll make it possible for the user to select the date range of data.

We will implement a horrendously complicated solution for this very basic need, but it'll prove useful to demonstrate and explain a concept that was introduced by ES2015 called generator functions. We actually mentioned this earlier in the chapter, when covering async and await.

Here's the code of the method:

loadYears(): void { console.log("Loading the years"); const retVal: number[] = []; const ...

Get Learn TypeScript 3 by Building Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.