future.js 959 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. /**
  3. * A future similar to Python's asyncio.Future. Allows to resolve or reject
  4. * outside of the executor and query the current status.
  5. */
  6. class Future extends Promise {
  7. constructor(executor) {
  8. let resolve, reject;
  9. super((resolve_, reject_) => {
  10. resolve = resolve_;
  11. reject = reject_;
  12. if (executor) {
  13. return executor(resolve_, reject_);
  14. }
  15. });
  16. this._done = false;
  17. this._resolve = resolve;
  18. this._reject = reject;
  19. }
  20. /**
  21. * Return whether the future is done (resolved or rejected).
  22. */
  23. get done() {
  24. return this._done;
  25. }
  26. /**
  27. * Resolve the future.
  28. */
  29. resolve(...args) {
  30. this._done = true;
  31. return this._resolve(...args);
  32. }
  33. /**
  34. * Reject the future.
  35. */
  36. reject(...args) {
  37. this._done = true;
  38. return this._reject(...args);
  39. }
  40. }