web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
847 B

  1. "use strict";
  2. var isFunction = require("../function/is-function");
  3. module.exports = function (executor) {
  4. var Constructor;
  5. if (isFunction(this)) {
  6. Constructor = this;
  7. } else if (typeof Promise === "function") {
  8. Constructor = Promise;
  9. } else {
  10. throw new TypeError("Could not resolve Promise constuctor");
  11. }
  12. var lazyThen;
  13. var promise = new Constructor(function (resolve, reject) {
  14. lazyThen = function (onSuccess, onFailure) {
  15. if (!hasOwnProperty.call(this, "then")) {
  16. // Sanity check
  17. throw new Error("Unexpected (inherited) lazy then invocation");
  18. }
  19. try { executor(resolve, reject); }
  20. catch (reason) { reject(reason); }
  21. delete this.then;
  22. return this.then(onSuccess, onFailure);
  23. };
  24. });
  25. return Object.defineProperty(promise, "then", {
  26. configurable: true,
  27. writable: true,
  28. value: lazyThen
  29. });
  30. };