UNPKG

2.85 kBMarkdownView Raw
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
8Lightweight ES6 Promise polyfill for the browser and node. Adheres closely to
9the spec. It is a perfect polyfill IE, Firefox or any other browser that does
10not support native promises.
11
12For API information about Promises, please check out this article
13[HTML5Rocks article](http://www.html5rocks.com/en/tutorials/es6/promises/).
14
15It is extremely lightweight. **_< 1kb Gzipped_**
16
17## Browser Support
18
19IE8+, Chrome, Firefox, IOS 4+, Safari 5+, Opera
20
21### NPM Use
22
23```
24npm install promise-polyfill --save-exact
25```
26
27### Bower Use
28
29```
30bower 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
47import Promise from 'promise-polyfill';
48```
49
50then you can use like normal Promises
51
52```js
53var 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
63prom.then(function(result) {
64 // Do something when async done
65});
66```
67
68If you would like to just polyfill, only if native doesn't exist.
69
70```js
71import 'promise-polyfill/src/polyfill';
72```
73
74## Performance
75
76By default promise-polyfill uses `setImmediate`, but falls back to `setTimeout`
77for executing asynchronously. If a browser does not support `setImmediate`
78(IE/Edge are the only browsers with setImmediate), you may see performance
79issues. 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
83If you polyfill `window.setImmediate` or use `Promise._immediateFn = yourImmediateFn` it will be used instead of `window.setTimeout`
84
85```
86npm install setasap --save
87```
88
89```js
90import Promise from 'promise-polyfill/src/polyfill';
91import setAsap from 'setasap';
92Promise._immediateFn = setAsap;
93```
94
95## Unhandled Rejections
96
97promise-polyfill will warn you about possibly unhandled rejections. It will show
98a console warning if a Promise is rejected, but no `.catch` is used. You can
99change this behavior by doing.
100
101```js
102Promise._unhandledRejectionFn = <your reject error handler>;
103```
104
105If you would like to disable unhandled rejection messages. Use a noop like
106below.
107
108```js
109Promise._unhandledRejectionFn = function(rejectError) {};
110```
111
112## Testing
113
114```
115npm install
116npm test
117```
118
119## License
120
121MIT