January 2019
Beginner
210 pages
4h 47m
English
To understand how the observable sequence pattern works, we also need to understand the iterator pattern. The following code snippet uses a generator to create an iterator that iterates the multiples of a given number in an array. Only the elements in the array that are multiples of a given number are iterated:
function* iterateOnMultiples(arr: number[], divisor: number) { for (let item of arr) { if (item % divisor === 0) { yield item; } }}
To get an instance of the iterator, we only need to invoke the function and pass an array and a number as its arguments. The function returns an iterator that will return the numbers in the array that are multiples of the given number: 3. We can invoke the iterator's next method to ...
Read now
Unlock full access