Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CLASSES */
- import './constants.ts';
- import './interfaces.ts';
- class PostManager {
- private posts: Post[] = [];
- private postIterator: IterableIterator<any>;
- constructor() {
- this.postIterator = this.fetchPost();
- }
- private *fetchPost() {
- let post_id: number = FIRST_POST_ID;
- while (post_id <= MAX_POST_COUNT) {
- yield fetch(`${HOST_URL}${post_id++}`).then(response => response.json());
- }
- }
- public collectNextPost() {
- let post = this.postIterator.next();
- if (post.done) throw new Error(`Maximum post limit is ${MAX_POST_COUNT}`);
- post.value.then(post => this.posts.push(post));
- return post.done;
- }
- public getPosts() {
- return this.posts;
- }
- }
Add Comment
Please, Sign In to add comment