Created
February 7, 2021 09:57
-
-
Save developerfromjokela/f64f2ec5cb6569652424b86e7b2b6e33 to your computer and use it in GitHub Desktop.
Async iterator for typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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