Guest User

Untitled

a guest
Jun 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /* CLASSES */
  2.  
  3. import './constants.ts';
  4. import './interfaces.ts';
  5.  
  6. class PostManager {
  7. private posts: Post[] = [];
  8. private postIterator: IterableIterator<any>;
  9.  
  10. constructor() {
  11. this.postIterator = this.fetchPost();
  12. }
  13.  
  14. private *fetchPost() {
  15. let post_id: number = FIRST_POST_ID;
  16.  
  17. while (post_id <= MAX_POST_COUNT) {
  18. yield fetch(`${HOST_URL}${post_id++}`).then(response => response.json());
  19. }
  20. }
  21.  
  22. public collectNextPost() {
  23. let post = this.postIterator.next();
  24.  
  25. if (post.done) throw new Error(`Maximum post limit is ${MAX_POST_COUNT}`);
  26.  
  27. post.value.then(post => this.posts.push(post));
  28. return post.done;
  29. }
  30.  
  31. public getPosts() {
  32. return this.posts;
  33. }
  34. }
Add Comment
Please, Sign In to add comment