GithubHelp home page GithubHelp logo

kryndex / promise-polyfill Goto Github PK

View Code? Open in Web Editor NEW

This project forked from taylorhakes/promise-polyfill

0.0 2.0 0.0 74 KB

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

License: MIT License

JavaScript 100.00%

promise-polyfill's Introduction

Promise Polyfill

travis

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.

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

Downloads

Simple use

import Promise from 'promise-polyfill'; 

// To add to window
if (!window.Promise) {
  window.Promise = Promise;
}

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
});

Deprecations

  • Promise._setImmediateFn(<immediateFn>) has been deprecated. Use Promise._immediateFn = <immediateFn>; instead.
  • Promise._setUnhandledRejectionFn(<rejectionFn>) has been deprecated. Use Promise._unhandledRejectionFn = <rejectionFn> instead. These functions will be removed in the next major version.

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
var Promise = require('promise-polyfill');
var setAsap = require('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 turn off this behavior by setting Promise._setUnhandledRejectionFn(<rejectError>). If you would like to disable unhandled rejections. Use a noop like below.

Promise._unhandledRejectionFn = function(rejectError) {};

Testing

npm install
npm test

License

MIT

promise-polyfill's People

Contributors

calummacuisdean avatar joseluisq avatar taylorhakes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.