Source: lib/util/public_promise.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.PublicPromise');
  18. /**
  19. * A utility to create Promises with convenient public resolve and reject
  20. * methods.
  21. *
  22. * @constructor
  23. * @struct
  24. * @extends {Promise.<T>}
  25. * @return {Promise.<T>}
  26. * @template T
  27. */
  28. shaka.util.PublicPromise = function() {
  29. let resolvePromise;
  30. let rejectPromise;
  31. // Promise.call causes an error. It seems that inheriting from a native
  32. // Promise is not permitted by JavaScript interpreters.
  33. // The work-around is to construct a Promise object, modify it to look like
  34. // the compiler's picture of PublicPromise, then return it. The caller of
  35. // new PublicPromise will receive |promise| instead of |this|, and the
  36. // compiler will be aware of the additional properties |resolve| and
  37. // |reject|.
  38. let promise = new Promise(function(resolve, reject) {
  39. resolvePromise = resolve;
  40. rejectPromise = reject;
  41. });
  42. promise.resolve = resolvePromise;
  43. promise.reject = rejectPromise;
  44. return promise;
  45. };
  46. /** @param {T=} value */
  47. shaka.util.PublicPromise.prototype.resolve = function(value) {};
  48. /** @param {*=} reason */
  49. shaka.util.PublicPromise.prototype.reject = function(reason) {};