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