Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AsyncIterator {
- iterations = 0;
- constructor(processor, iterations, callback) {
- this.processor = processor;
- this.callback = callback;
- this.maxIterations = iterations;
- this.tick();
- }
- async tick() {
- return new Promise((s, f) => {
- setTimeout(() => {
- this.processor(this.iterations);
- s();
- })
- }).then(() => {
- if (this.iterations < this.maxIterations) {
- this.iterations++;
- this.tick();
- } else {
- this.callback()
- }
- })
- }
- }
- const iTest = new AsyncIterator(i => {
- document.write(i + '<br>');
- }, 100, () => {
- console.log('Done');
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement