• Stars
    star
    2,129
  • Rank 21,679 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight ES6 Promise polyfill for the browser and node. A+ Compliant

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

More Repositories

1

fecha

Lightweight and simple JS date formatting and parsing
JavaScript
2,043
star
2

python-redis-cache

Simple redis cache for Python functions
Python
68
star
3

painless

Painless Test Library - Easy to learn, use and debug
JavaScript
58
star
4

setAsap

setImmediate polyfill for the browser and node
JavaScript
42
star
5

variable-diff

Visual diff between 2 javascript variables
JavaScript
31
star
6

node-systemd-selinux

Files to setup Node JS running on Systemd and SELinux
Shell
14
star
7

sparkgrid

WIP - A blazingly fast Javascript data grid
JavaScript
12
star
8

promise-mock

Promise mocking library to make Promises resolve synchronously
JavaScript
11
star
9

localstorage-lock

Generic localstorage lock implementation
JavaScript
9
star
10

postmessage-plus

Simple postmessage library
JavaScript
6
star
11

html5-sortable

HTML5 Sortable without jQuery
JavaScript
5
star
12

Plex-TV-Analyzer

Plex-TV-Analyzer is a simple web page that will analyze a television show in your Plex library and tell you which episodes you are missing. Written as a web application. Must have PHP installed to use
JavaScript
5
star
13

clock-interval

A precise interval timer
JavaScript
5
star
14

swiftpreview

Preview any link in Chrome
JavaScript
4
star
15

observable

Lightweight observable class (similar to watchjs) for use with rivetsjs and other libraries
JavaScript
4
star
16

PHP-Factory-Generator

Generates PHP code for a MYSQL database allowing MYSQL tables to be used as objects. Also creates functions for creating, retrieving and deleting objects from the database.
PHP
3
star
17

multitrakt

Rate TMDB and IMDB when rating on Trakt.tv
JavaScript
2
star
18

redux-slidedeck

Slide deck library built with redux
JavaScript
2
star
19

taylorhakes.com

My website
CSS
1
star
20

js-experiments

1
star
21

logger

Comprehensive javascript logging
JavaScript
1
star
22

onNodesInserted

Get notified when DOM node is inserted
JavaScript
1
star
23

flux-presentation

Presentation on Flux
JavaScript
1
star
24

react-starter

React, Redux, Observables, Webpack starter kit
JavaScript
1
star
25

etsy-search

Search the Etsy API with a simple webpage
JavaScript
1
star
26

javascript-oop-presentation

Javascript OOP Presentation
JavaScript
1
star