您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Promise Polyfill
  2. [![travis][travis-image]][travis-url]
  3. [travis-image]: https://img.shields.io/travis/taylorhakes/promise-polyfill.svg?style=flat
  4. [travis-url]: https://travis-ci.org/taylorhakes/promise-polyfill
  5. Lightweight ES6 Promise polyfill for the browser and node. Adheres closely to
  6. the spec. It is a perfect polyfill IE, Firefox or any other browser that does
  7. not support native promises.
  8. For API information about Promises, please check out this article
  9. [HTML5Rocks article](http://www.html5rocks.com/en/tutorials/es6/promises/).
  10. It is extremely lightweight. **_< 1kb Gzipped_**
  11. ## Browser Support
  12. IE8+, Chrome, Firefox, IOS 4+, Safari 5+, Opera
  13. ### NPM Use
  14. ```
  15. npm install promise-polyfill --save-exact
  16. ```
  17. ### Bower Use
  18. ```
  19. bower install promise-polyfill
  20. ```
  21. ### CDN Polyfill Use
  22. This will set a global Promise object if the browser doesn't already have `window.Promise`.
  23. ```html
  24. <script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
  25. ```
  26. ## Downloads
  27. * [Promise](https://raw.github.com/taylorhakes/promise-polyfill/master/dist/polyfill.js)
  28. * [Promise-min](https://raw.github.com/taylorhakes/promise-polyfill/master/dist/polyfill.min.js)
  29. ## Simple use
  30. If you would like to add a global Promise object (Node or Browser) if native Promise doesn't exist (polyfill Promise). Use the method below. This is useful it you are building a website and want to support older browsers.
  31. Javascript library authors should _NOT_ use this method.
  32. ```js
  33. import 'promise-polyfill/src/polyfill';
  34. ```
  35. If you would like to not affect the global environment (sometimes known as a [ponyfill](ponyfill.com)), you can import the base module. This is nice for library authors or people working in environment where you don't want
  36. to affect the global environment.
  37. ```js
  38. import Promise from 'promise-polyfill';
  39. ```
  40. If using `require` with Webpack 2+ (rare), you need to specify the default import
  41. ```js
  42. var Promise = require('promise-polyfill').default;
  43. ```
  44. then you can use like normal Promises
  45. ```js
  46. var prom = new Promise(function(resolve, reject) {
  47. // do a thing, possibly async, then…
  48. if (/* everything turned out fine */) {
  49. resolve("Stuff worked!");
  50. } else {
  51. reject(new Error("It broke"));
  52. }
  53. });
  54. prom.then(function(result) {
  55. // Do something when async done
  56. });
  57. ```
  58. ## Performance
  59. By default promise-polyfill uses `setImmediate`, but falls back to `setTimeout`
  60. for executing asynchronously. If a browser does not support `setImmediate`
  61. (IE/Edge are the only browsers with setImmediate), you may see performance
  62. issues. Use a `setImmediate` polyfill to fix this issue.
  63. [setAsap](https://github.com/taylorhakes/setAsap) or
  64. [setImmediate](https://github.com/YuzuJS/setImmediate) work well.
  65. If you polyfill `window.setImmediate` or use `Promise._immediateFn = yourImmediateFn` it will be used instead of `window.setTimeout`
  66. ```
  67. npm install setasap --save
  68. ```
  69. ```js
  70. import Promise from 'promise-polyfill/src/polyfill';
  71. import setAsap from 'setasap';
  72. Promise._immediateFn = setAsap;
  73. ```
  74. ## Unhandled Rejections
  75. promise-polyfill will warn you about possibly unhandled rejections. It will show
  76. a console warning if a Promise is rejected, but no `.catch` is used. You can
  77. change this behavior by doing.
  78. -**NOTE: This only works on promise-polyfill Promises. Native Promises do not support this function**
  79. ```js
  80. Promise._unhandledRejectionFn = <your reject error handler>;
  81. ```
  82. If you would like to disable unhandled rejection messages. Use a noop like
  83. below.
  84. ```js
  85. Promise._unhandledRejectionFn = function(rejectError) {};
  86. ```
  87. ## Testing
  88. ```
  89. npm install
  90. npm test
  91. ```
  92. ## License
  93. MIT