polyfill.js 724 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*global self*/
  2. import Promise from './promise';
  3. export default function polyfill() {
  4. let local;
  5. if (typeof global !== 'undefined') {
  6. local = global;
  7. } else if (typeof self !== 'undefined') {
  8. local = self;
  9. } else {
  10. try {
  11. local = Function('return this')();
  12. } catch (e) {
  13. throw new Error('polyfill failed because global object is unavailable in this environment');
  14. }
  15. }
  16. let P = local.Promise;
  17. if (P) {
  18. var promiseToString = null;
  19. try {
  20. promiseToString = Object.prototype.toString.call(P.resolve());
  21. } catch(e) {
  22. // silently ignored
  23. }
  24. if (promiseToString === '[object Promise]' && !P.cast){
  25. return;
  26. }
  27. }
  28. local.Promise = Promise;
  29. }