If we invoke a function in TypeScript, we can assume that, once the function starts running, it will always run to completion before any other code can run. However, one type of function known as a generator can may be paused in the middle of execution—once or many times—and resumed later, allowing other code to run during these paused periods.
A generator represents a sequence of values. The interface of a generator object is just an iterator. An iterator implements the following interface:
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
The next function can be invoked until it runs out of values. We can define a generator by using the function keyword, followed by an asterisk, (*). The yield keyword is used to stop the execution of...