Releases: T99/iter-over
Fixed Iterator Type
After changes made for the v2.0.0 release, the type being returned for [Symbol.iterator]
included undefined
in the type union for the iterator result. This caused users to have to cast iteration results to their intended type:
for (person of new PersonIterator(people)) {
let person: Person = person as Person;
}
Otherwise, TypeScript would complain that Person | undefined
could not be assigned to a variable of type Person
.
Added `AsyncIterator`
Added the AsyncIterator
class to support the (somewhat) new [Symbol.asyncIterator]
protocol. This new class allows for iteration over an array of Promises using the for await
syntax:
async function fetchWeatherForCity(city: string): Promise<WeatherData> { ... }
let weatherAsyncIterable: AsyncIterable<WeatherData> = new AsyncIterable([
fetchWeatherForCity("Chicago"),
fetchWeatherForCity("New York"),
fetchWeatherForCity("Seattle"),
fetchWeatherForCity("Miami")
]);
for await (weatherData of weatherAsyncIterable) {
console.log(`${weatherData.location}: ${weatherData.temperature}`);
}
The above code would print out the temperature in each city, as the data becomes available. This is to say that if whatever API we are using to get weather data responds with data for Seattle in 500ms, but then takes 4 seconds to return us data for Chicago, the Seattle data will be printed as soon as it becomes available, and the same goes for the Chicago data.
The above code is akin to doing something like the following:
async function fetchWeatherForCity(city: string): Promise<WeatherData> { ... }
let weatherDataPromises: Promise<any>[] = [
fetchWeatherForCity("Chicago"),
fetchWeatherForCity("New York"),
fetchWeatherForCity("Seattle"),
fetchWeatherForCity("Miami")
];
weatherDataPromises.forEach((weatherDataPromise: Promise<WeatherData>): void => {
console.log(`${weatherData.location}: ${weatherData.temperature}`);
});
await Promise.all(weatherDataPromises);
But who doesn't like using new shiny syntax? 😄
Addition of Inline Documentation
Added documentation and #getIterableIterator method. Also modified return type of base #next method.
The Renaming
After working with importing my own modules from NPM more, I've realized that prefixing the files I'd published wasn't really convention or even all that useful in the end. Files have been renamed accordingly.
IOAIterator
⟶AbstractIterator
IOIIterator
⟶IIterator
IOStringIterator
⟶StringIterator
IOEmptyIterator
⟶EmptyIterator
IOObjectIterator
⟶ObjectIterator
IOKeyValuePair
⟶KeyValuePair
Find this version on NPM.
Initial Release
This is the first working version to be live and available on NPM, including the following classes:
IOAIterator
IOIIterator
IOStringIterator
IOEmptyIterator
IOObjectIterator
IOKeyValuePair