1 | <a href="http://promises-aplus.github.com/promises-spec">
|
2 | <img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png"
|
3 | align="right" alt="Promises/A+ logo" />
|
4 | </a>
|
5 | # Promise Polyfill
|
6 | [![travis][travis-image]][travis-url]
|
7 |
|
8 | [travis-image]: https://img.shields.io/travis/taylorhakes/promise-polyfill.svg?style=flat
|
9 | [travis-url]: https://travis-ci.org/taylorhakes/promise-polyfill
|
10 |
|
11 |
|
12 | 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.
|
13 |
|
14 | For API information about Promises, please check out this article [HTML5Rocks article](http://www.html5rocks.com/en/tutorials/es6/promises/).
|
15 |
|
16 | It is extremely lightweight. ***< 1kb Gzipped***
|
17 |
|
18 | ## Browser Support
|
19 | IE8+, Chrome, Firefox, IOS 4+, Safari 5+, Opera
|
20 |
|
21 | ### NPM Use
|
22 | ```
|
23 | npm install promise-polyfill --save-exact
|
24 | ```
|
25 | ### Bower Use
|
26 | ```
|
27 | bower install promise-polyfill
|
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 | var prom = new Promise(function(resolve, reject) {
|
38 | // do a thing, possibly async, then…
|
39 |
|
40 | if (/* everything turned out fine */) {
|
41 | resolve("Stuff worked!");
|
42 | } else {
|
43 | reject(new Error("It broke"));
|
44 | }
|
45 | });
|
46 |
|
47 | prom.then(function(result) {
|
48 | // Do something when async done
|
49 | });
|
50 | ```
|
51 |
|
52 | ## Performance
|
53 | 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.
|
54 | Use a `setImmediate` polyfill to fix this issue. [setAsap](https://github.com/taylorhakes/setAsap) or [setImmediate](https://github.com/YuzuJS/setImmediate) work well.
|
55 |
|
56 | If you polyfill `window.setImmediate` or use `Promise._setImmediateFn(immedateFn)` it will be used instead of `window.setTimeout`
|
57 | ```
|
58 | npm install setasap --save
|
59 | ```
|
60 | ```js
|
61 | var Promise = require('promise-polyfill');
|
62 | var setAsap = require('setasap');
|
63 | Promise._setImmediateFn(setAsap);
|
64 | ```
|
65 |
|
66 | ## Unhandled Rejections
|
67 | 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>)`.
|
68 | If you would like to disable unhandled rejections. Use a noop like below.
|
69 | ```js
|
70 | Promise._setUnhandledRejectionFn(function(rejectError) {});
|
71 | ```
|
72 |
|
73 |
|
74 | ## Testing
|
75 | ```
|
76 | npm install
|
77 | npm test
|
78 | ```
|
79 |
|
80 | ## License
|
81 | MIT
|