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