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 | ## Deprecations
|
53 | - `Promise._setImmediateFn(<immediateFn>)` has been deprecated. Use `Promise._immediateFn = <immediateFn>;` instead.
|
54 | - `Promise._setUnhandledRejectionFn(<rejectionFn>)` has been deprecated. Use `Promise._unhandledRejectionFn = <rejectionFn>` instead.
|
55 | These functions will be removed in the next major version.
|
56 |
|
57 | ## Performance
|
58 | 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.
|
59 | Use a `setImmediate` polyfill to fix this issue. [setAsap](https://github.com/taylorhakes/setAsap) or [setImmediate](https://github.com/YuzuJS/setImmediate) work well.
|
60 |
|
61 | If you polyfill `window.setImmediate` or use `Promise._immediateFn = yourImmediateFn` it will be used instead of `window.setTimeout`
|
62 | ```
|
63 | npm install setasap --save
|
64 | ```
|
65 | ```js
|
66 | var Promise = require('promise-polyfill');
|
67 | var setAsap = require('setasap');
|
68 | Promise._immediateFn = setAsap;
|
69 | ```
|
70 |
|
71 | ## Unhandled Rejections
|
72 | 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>)`.
|
73 | If you would like to disable unhandled rejections. Use a noop like below.
|
74 | ```js
|
75 | Promise._unhandledRejectionFn = function(rejectError) {};
|
76 | ```
|
77 |
|
78 |
|
79 | ## Testing
|
80 | ```
|
81 | npm install
|
82 | npm test
|
83 | ```
|
84 |
|
85 | ## License
|
86 | MIT
|