taylorhakes/promise-polyfill
Fork: 313 Star: 2158 (更新于 2024-10-29 16:45:21)
license: MIT
Language: JavaScript .
Lightweight ES6 Promise polyfill for the browser and node. A+ Compliant
最后发布版本: 8.2.2 ( 2022-03-12 14:59:17)
Promise Polyfill
Lightweight ES6 Promise polyfill for the browser and node. Adheres closely to the spec. It is a perfect polyfill IE or any other browser that does not support native promises.
For API information about Promises, please check out this article HTML5Rocks article.
It is extremely lightweight. < 1kb Gzipped
Browser Support
IE8+, Chrome, Firefox, IOS 4+, Safari 5+, Opera
NPM Use
npm install promise-polyfill --save-exact
Bower Use
bower install promise-polyfill
CDN Polyfill Use
This will set a global Promise object if the browser doesn't already have window.Promise
.
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
Downloads
Simple use
If you would like to add a global Promise object (Node or Browser) if native Promise doesn't exist (polyfill Promise). Use the method below. This is useful if you are building a website and want to support older browsers. Javascript library authors should NOT use this method.
import 'promise-polyfill/src/polyfill';
If you would like to not affect the global environment (sometimes known as a ponyfill, you can import the base module. This is nice for library authors or people working in environment where you don't want to affect the global environment.
import Promise from 'promise-polyfill';
If using require
with Webpack 2+ (rare), you need to specify the default import
var Promise = require('promise-polyfill').default;
then you can use like normal Promises
var prom = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (/* everything turned out fine */) {
resolve("Stuff worked!");
} else {
reject(new Error("It broke"));
}
});
prom.then(function(result) {
// Do something when async done
});
Performance
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. Use a setImmediate
polyfill to fix this issue.
setAsap or
setImmediate work well.
If you polyfill window.setImmediate
or use Promise._immediateFn = yourImmediateFn
it will be used instead of window.setTimeout
npm install setasap --save
import Promise from 'promise-polyfill/src/polyfill';
import setAsap from 'setasap';
Promise._immediateFn = setAsap;
Unhandled Rejections
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
change this behavior by doing.
-NOTE: This only works on promise-polyfill Promises. Native Promises do not support this function
Promise._unhandledRejectionFn = <your reject error handler>;
If you would like to disable unhandled rejection messages. Use a noop like below.
Promise._unhandledRejectionFn = function(rejectError) {};
Testing
npm install
npm test
License
MIT
最近版本更新:(数据更新于 2024-10-09 09:52:07)
2022-03-12 14:59:17 8.2.2
2020-11-03 12:26:27 8.2.0
2019-06-17 05:51:50 8.1.3
2019-06-15 12:26:55 8.1.2
2019-06-14 14:46:35 8.1.1
2018-08-12 00:24:38 8.1.0
2018-08-01 13:19:44 8.0.0
2018-04-05 21:41:00 7.1.2
2018-02-07 22:03:20 7.1.0
2018-01-25 21:58:23 7.0.2
taylorhakes/promise-polyfill同语言 JavaScript最近更新仓库
2024-11-05 19:13:47 jerryc127/hexo-theme-butterfly
2024-11-05 13:53:42 LiteLoaderQQNT/LiteLoaderQQNT
2024-11-03 02:40:36 chris81605/Degrees-of-Lewdity_Cheat_Extended
2024-11-01 21:55:46 projectdiscovery/nuclei-templates
2024-11-01 19:24:44 NumberSir/DoL-I18n-Build
2024-11-01 12:25:14 midoks/mdserver-web