Skip to content

Instantly share code, notes, and snippets.

@developerfromjokela
Created February 7, 2021 09:57
Show Gist options
  • Save developerfromjokela/f64f2ec5cb6569652424b86e7b2b6e33 to your computer and use it in GitHub Desktop.
Save developerfromjokela/f64f2ec5cb6569652424b86e7b2b6e33 to your computer and use it in GitHub Desktop.
Async iterator for typescript
/*
* Copyright (c) 2021 wilmaplus-notifier2, developed by @developerfromjokela, for Wilma Plus mobile app
*/
export class AsyncIterator<T> {
currentItem = -1
items: T[]
callback:(item: T, iterator: AsyncIterator<T>) => void;
endCallback:() => void;
constructor(callback:(item: T, iterator: AsyncIterator<T>) => void, items:T[], endCallback: () => void) {
this.items = items;
this.callback = callback;
this.endCallback = endCallback;
}
nextItem() {
if (this.currentItem+1 < this.items.length) {
this.currentItem++;
this.callback(this.items[this.currentItem], this);
} else {
this.endCallback();
}
}
start = this.nextItem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment