Created
August 12, 2019 13:25
-
-
Save hubgit/59c0defcb16ea3ffeb90b9f6e0a03b78 to your computer and use it in GitHub Desktop.
A queue
This file contains 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
type QueueItem = () => Promise<void> | |
export class Queue { | |
private items: QueueItem[] = [] | |
private running: boolean = false | |
public add = (item: QueueItem): void => { | |
this.items.push(item) | |
if (!this.running) { | |
this.run() | |
} | |
} | |
private run = (): void => { | |
this.running = true | |
const item = this.items.shift() | |
if (item) { | |
item().finally(this.run) | |
} else { | |
this.running = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: