deferred.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. /**
  3. * Copyright 2022 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.Deferred = void 0;
  20. class Deferred {
  21. #isFinished = false;
  22. #promise;
  23. #resolve;
  24. #reject;
  25. get isFinished() {
  26. return this.#isFinished;
  27. }
  28. constructor() {
  29. this.#promise = new Promise((resolve, reject) => {
  30. this.#resolve = resolve;
  31. this.#reject = reject;
  32. });
  33. // Needed to avoid `Uncaught (in promise)`. The promises returned by `then`
  34. // and `catch` will be rejected anyway.
  35. this.#promise.catch((_error) => {
  36. // Intentionally empty.
  37. });
  38. }
  39. then(onFulfilled, onRejected) {
  40. return this.#promise.then(onFulfilled, onRejected);
  41. }
  42. catch(onRejected) {
  43. return this.#promise.catch(onRejected);
  44. }
  45. resolve(value) {
  46. this.#isFinished = true;
  47. this.#resolve?.(value);
  48. }
  49. reject(reason) {
  50. this.#isFinished = true;
  51. this.#reject?.(reason);
  52. }
  53. finally(onFinally) {
  54. return this.#promise.finally(onFinally);
  55. }
  56. [Symbol.toStringTag] = 'Promise';
  57. }
  58. exports.Deferred = Deferred;
  59. //# sourceMappingURL=deferred.js.map