Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class Queue {
  2.  
  3. constructor (id) {
  4. this.id = id;
  5. this.weight = 0;
  6. this.isClosed = false;
  7. this._p = Promise.resolve();
  8. }
  9.  
  10. add (weight, task) {
  11. this.weight += weight;
  12. this._p = this._p
  13. .then(() => {
  14. if (this.isClosed) return;
  15. log(`Queue #${this.id}: task is started`);
  16. return task();
  17. })
  18. .catch(log)
  19. .then(() => {
  20. this.weight -= weight;
  21. if (this.isClosed) return;
  22. log(`Queue #${this.id}: task is finished`);
  23. });
  24. }
  25.  
  26. close () {
  27. this.isClosed = true;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement