Git Product home page Git Product logo

es6-promise's Introduction

ES6-Promise (subset of rsvp.js) Build Status

This is a polyfill of the ES6 Promise. The implementation is a subset of rsvp.js extracted by @jakearchibald, if you're wanting extra features and more debugging options, check out the full library.

For API details and how to use promises, see the JavaScript Promises HTML5Rocks article.

Downloads

CDN

To use via a CDN include this in your html:

<!-- Automatically provides/replaces `Promise` if missing or broken. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script> 

<!-- Minified version of `es6-promise-auto` below. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script> 

Node.js

To install:

yarn add es6-promise

or

npm install es6-promise

To use:

var Promise = require('es6-promise').Promise;

Usage in IE<9

catch and finally are reserved keywords in IE<9, meaning promise.catch(func) or promise.finally(func) throw a syntax error. To work around this, you can use a string to access the property as shown in the following example.

However most minifiers will automatically fix this for you, making the resulting code safe for old browsers and production:

promise['catch'](function(err) {
  // ...
});
promise['finally'](function() {
  // ...
});

Auto-polyfill

To polyfill the global environment (either in Node or in the browser via CommonJS) use the following code snippet:

require('es6-promise').polyfill();

Alternatively

require('es6-promise/auto');

Notice that we don't assign the result of polyfill() to any variable. The polyfill() method will patch the global environment (in this case to the Promise name) when called.

Building & Testing

You will need to have PhantomJS installed globally in order to run the tests.

npm install -g phantomjs

  • npm run build to build
  • npm test to run tests
  • npm start to run a build watcher, and webserver to test
  • npm run test:server for a testem test runner and watching builder

es6-promise's People

Contributors

azizhk avatar bekzod avatar cyril-sf avatar domenic avatar fivetanley avatar greenkeeperio-bot avatar jakearchibald avatar josh avatar kt3k avatar lucretiel avatar machty avatar mikepmunroe avatar notslang avatar pangratz avatar rwaldron avatar rwjblue avatar ryanflorence avatar stefanpenner avatar teddyzeenny avatar tomdale avatar tricknotes avatar twokul avatar ukyo avatar unional avatar vicb avatar wagenet avatar webreflection avatar wibblymat avatar wkovacs64 avatar wycats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

es6-promise's Issues

Helper method for node functions

I find myself defining this in every single source file when I'm using promises in NodeJS:

function asp(fn) {
  return function() {
    var self = this;
    var args = Array.prototype.splice.call(arguments, 0);
    return new Promise(function(resolve, reject) {
      args.push(function(err, val) {
        if (err)
          return reject(err);
        resolve(val);
      });
      fn.apply(self, args);
    });    
  }
}

So you can do things like:

  asp(fs.readFile)('some/file').then(function(content) {
  });

Does this seem sensible? Is there a helper that already does this? Can we make a helper that does so this solution doesn't need to be repeated?

Possible to make it synchronous for testing?

It's really nice to deal with synchronous code in a testing context. How difficult do you think it would be to make it possible to mock out a backend and make the api synchronous?

Automatic polyfill

It seems this polyfill requires you to call ES6Promise.polyfill() before it will work. Wouldn't it be better if it created a global Promise if it cannot find the global Promise?

Error stops execution

I have following function which returns a promise:

function getStats (filePath) {
    return new Promise(function (fulfill, reject) {
        fs.stat(filePath, function (error, stats) {
            if (error)
                reject(error)
            else
                fulfill(stats)
        })
    })
}

Unfortunately I forgot to require fs before.
The outcome was, that the execution of the code stopped when I used the promise later.
Is this the correct behavior and how am I supposed to catch the exception then?

race() and all() must use Promise.resolve() internally

Unless I'm missing something, the current implementation does not actually use Promise.resolve(..) (aka, this.resolve()) inside of Promise.race(..) and Promise.all(..). The spec requires it. Having a duplicated inlining of the same logic (which is what I think I see happening in the code) is not sufficient.

See the first 2-3 items in this thread between me and @domenic where he confirms the same: https://gist.github.com/domenic/43d3228e141ea225ea6e

Also, aside from being a violation of the spec, the side effect of having the logic separated is actually related to #26.

Per that bug, if you fix Promise.resolve(..) to call then on a scheduled cycle rather than synchronously, but leave Promise.race(..) and Promise.all(..) as not using Promise.resolve(..), then both Promise.race(..) and Promise.all(..) will ALSO be incorrect for the same type of bug.

Here's two tests that illustrate that problem with Promise.race(..) and Promise.all(..):

Tag Versions

I see that there are release changes in the versions released via npm in some places and build details, but no tags.
Tagging would be good for tracking as well as people not using npm for version tracking.

does not work on Nashorn

Due to the lack of setTimeout (and all the other alternatives).

(Nashorn is the successor of Rhino).

Uncaught Error Output

During development it would be important to see uncaught errors (starting from compile errors!). RSVP allows to react on uncaught errors, here's for example the snippet we are using (this leads to a good output in Chrome - there may be better ways in other browsers / cross browser):

RSVP.on('error', function(d) {
    console.log("uncatched rejection of promise", d);
    if(d instanceof Error && d.stack) console.error(d.stack);
});

Could it be an option to either include this feature in the Polyfill or provide a feature to add an uncaught error handler (with the knowledge that this is not ES6).

Polyfill without clobbering?

I'm aware that I can call polyfill() to create global.Promise if it doesn't already exist, but messing with the global object makes me itchy, especially if my code isn't the only code running in the system. (e.g. some less responsible library puts an incompatible Promise implementation into the global object and I'm clobbering it, or it clobbers me).

Would it make sense for require("es6-promise").Promise to return the native implementation if it is available, and only return the RSVP-ish version if it isn't? That way I can cheerfully use var Promise = require("es6-promise").Promise; without worrying I'm going to glitch the system somewhere else.

How do I use the polyfill with a browser

Sorry if this is a really silly question, I'm not experienced with ecmascript, but I have looked around and I cannot get the polyfill into any javascript in a browser. I'm keen to add Promise to firefox. If I use the example (from the closed issue http://bit.ly/1iv61dm , ff either says that's a syntax error if I embed the code into a <script> section, or complains that import must be at the top level if I drop it into a file and browserify it, and then paste that into my js code.

Async support

Dispatching an event at the end of initialization makes this script asynchronously usable.
In the browser:
<script async src="promise.polyfill.js">

E.g.

//dispatch an event so this script can be used asynchronously
try {
    dispatchEvent(new CustomEvent(/*propose*/ "Promise"));
} catch (e) {
    console.error(e);
}

"You must pass a resolver function as the first argument to the promise constructor"

Why is this a thing?

I have researched this a little bit and I can't seem to find out why this is necessary at all. Can someone explain why you need to pass a function to the Promise constructor?

Is there some reason we cannot create a Promise or Deferred object that we will manually resolve at a later point?

For example, in jQuery, you can do the following, and it is extremely useful in multiple scenarios:

var deferred = new $.Deferred(); //works in jquery to give you a deferred object you can control manually

var deferred = new Promise(); //fails in the es6 Promise polyfill with the subject of this issue as an error msg.

Can we not perform a simple check on the Promise constructor, and return an object with a resolve and reject function if no function was passed? Why the need to wrap our functions in the promise constructor?

Native Browser Support

I'm not sure if this polyfill can be used directly in a browser without some third-party module system. Can you add support for vanilla browser usage, if that's missing, and documentation on how to use the polyfill in that situation?

Thanks!

IE10 Failure (polyfill function not triggered)

I've met an issue testing the polyfill under IE10/11

SCRIPT5009: « Promise » is undefined

Maybe I misunderstand the usage but it seems that the polyfill function $$es6$promise$polyfill$$default is not triggered.

Thanks.

WIP update

1.69KB gzipped (4.05KB uncompressed) with closure compiler, fucking fast, and many bug fixes.

the majority of the file-size savings (this version of promises is actually many more bytes then the original) is thanks to @eventualbuddha's latest "bundled" output for es6-module-transpilation

https://github.com/tildeio/rsvp.js/tree/es6-promise

  • improve build step
  • ensure tests are also the correct subset and run.
  • ... ?

@jakearchibald unsure if you have time, but extra help here would be great :)

some micro-benchmarks (take them with a grain of salt)
also note: es6Promise is the existing implementation, es6 is the new one (and basically rsvp-mini)
also also note: any=place where es6Promise appears to be faster, is actually a spec violation bug in es6Promise.

clearly some parts can still be improved, but good progress. Then sequence is one of poor areas right now.

Running Benchmark Suite for test - All 1 ...
- [All 1] es6 x 1,317,949 ops/sec ±17.55% (54 runs sampled)
- [All 1] es6Promise x 94,532 ops/sec ±27.26% (48 runs sampled)
- [All 1] bluebird x 618,668 ops/sec ±19.72% (56 runs sampled)
- [All 1] when x 1,168,224 ops/sec ±12.74% (65 runs sampled)
Running Benchmark Suite for test - All 2 ...
- [All 2] es6 x 1,287,601 ops/sec ±21.85% (54 runs sampled)
- [All 2] es6Promise x 125,851 ops/sec ±6.66% (53 runs sampled)
- [All 2] bluebird x 666,175 ops/sec ±9.68% (64 runs sampled)
- [All 2] when x 968,931 ops/sec ±6.29% (65 runs sampled)
Running Benchmark Suite for test - All 10 ...
- [All 10] es6 x 1,337,632 ops/sec ±13.48% (60 runs sampled)
- [All 10] es6Promise x 109,120 ops/sec ±12.73% (50 runs sampled)
- [All 10] bluebird x 378,692 ops/sec ±15.93% (58 runs sampled)
- [All 10] when x 777,846 ops/sec ±16.70% (54 runs sampled)
Running Benchmark Suite for test - All 50 ...
- [All 50] es6 x 634,875 ops/sec ±17.05% (54 runs sampled)
- [All 50] es6Promise x 73,311 ops/sec ±20.88% (49 runs sampled)
- [All 50] bluebird x 189,959 ops/sec ±17.09% (60 runs sampled)
- [All 50] when x 707,446 ops/sec ±22.26% (61 runs sampled)
Running Benchmark Suite for test - All 100 ...
- [All 100] es6 x 462,885 ops/sec ±16.54% (58 runs sampled)
- [All 100] es6Promise x 65,346 ops/sec ±14.34% (56 runs sampled)
- [All 100] bluebird x 116,115 ops/sec ±15.08% (66 runs sampled)
- [All 100] when x 576,561 ops/sec ±10.43% (66 runs sampled)
Running Benchmark Suite for test - All 1000 ...
- [All 1000] es6 x 56,181 ops/sec ±12.05% (64 runs sampled)
- [All 1000] es6Promise x 12,583 ops/sec ±16.04% (51 runs sampled)
- [All 1000] bluebird x 15,053 ops/sec ±11.64% (67 runs sampled)
- [All 1000] when x 110,630 ops/sec ±15.70% (61 runs sampled)
Running Benchmark Suite for test - All 10000 ...
- [All 10000] es6 x 7,940 ops/sec ±14.69% (67 runs sampled)
- [All 10000] es6Promise x 1,329 ops/sec ±17.70% (57 runs sampled)
- [All 10000] bluebird x 1,478 ops/sec ±12.85% (64 runs sampled)
- [All 10000] when x 13,102 ops/sec ±16.27% (66 runs sampled)
Running Benchmark Suite for test - All thenable 10000 ...
- [All thenable 10000] es6 x 2,396 ops/sec ±7.11% (68 runs sampled)
- [All thenable 10000] es6Promise x 42.97 ops/sec ±13.67% (46 runs sampled)
- [All thenable 10000] bluebird x 1,446 ops/sec ±6.25% (66 runs sampled)
- [All thenable 10000] when x 3,470 ops/sec ±7.91% (70 runs sampled)
Running Benchmark Suite for test - Creation 1 ...
- [Creation 1] es6 x 6,043,403 ops/sec ±10.91% (58 runs sampled)
- [Creation 1] es6Promise x 6,406,419 ops/sec ±9.21% (62 runs sampled)
- [Creation 1] bluebird x 4,741,041 ops/sec ±12.83% (60 runs sampled)
- [Creation 1] when x 2,924,957 ops/sec ±13.53% (60 runs sampled)
Running Benchmark Suite for test - Reject 1 ...
- [Reject 1] es6 x 232,715 ops/sec ±8.04% (47 runs sampled)
- [Reject 1] es6Promise x 359,730 ops/sec ±13.03% (46 runs sampled)
- [Reject 1] bluebird x 267,141 ops/sec ±8.95% (65 runs sampled)
- [Reject 1] when x 877,527 ops/sec ±11.80% (63 runs sampled)
Running Benchmark Suite for test - Resolve 1 ...
- [Resolve 1] es6 x 1,220,709 ops/sec ±8.89% (60 runs sampled)
- [Resolve 1] es6Promise x 358,367 ops/sec ±11.29% (55 runs sampled)
- [Resolve 1] bluebird x 1,204,362 ops/sec ±7.85% (66 runs sampled)
- [Resolve 1] when x 1,126,115 ops/sec ±9.00% (64 runs sampled)
Running Benchmark Suite for test - Resolve Promise 1 ...
- [Resolve Promise 1] es6 x 813,110 ops/sec ±25.63% (53 runs sampled)
- [Resolve Promise 1] es6Promise x 147,867 ops/sec ±21.03% (47 runs sampled)
- [Resolve Promise 1] bluebird x 1,188,509 ops/sec ±16.24% (60 runs sampled)
- [Resolve Promise 1] when x 1,311,467 ops/sec ±15.12% (62 runs sampled)
Running Benchmark Suite for test - Then sequence 1 ...
- [Then sequence 1] es6 x 939,479 ops/sec ±18.17% (55 runs sampled)
- [Then sequence 1] es6Promise x 221,425 ops/sec ±17.20% (51 runs sampled)
- [Then sequence 1] bluebird x 1,062,344 ops/sec ±17.98% (60 runs sampled)
- [Then sequence 1] when x 1,368,123 ops/sec ±20.26% (57 runs sampled)
Running Benchmark Suite for test - Then sequence 2 ...
- [Then sequence 2] es6 x 647,827 ops/sec ±15.82% (54 runs sampled)
- [Then sequence 2] es6Promise x 157,352 ops/sec ±20.13% (40 runs sampled)
- [Then sequence 2] bluebird x 908,095 ops/sec ±16.80% (62 runs sampled)
- [Then sequence 2] when x 1,235,377 ops/sec ±14.46% (62 runs sampled)
Running Benchmark Suite for test - Then sequence 10 ...
- [Then sequence 10] es6 x 208,993 ops/sec ±21.62% (53 runs sampled)
- [Then sequence 10] es6Promise x 50,187 ops/sec ±21.17% (42 runs sampled)
- [Then sequence 10] bluebird x 334,191 ops/sec ±18.08% (60 runs sampled)
- [Then sequence 10] when x 890,853 ops/sec ±15.25% (66 runs sampled)
Running Benchmark Suite for test - Then sequence 50 ...
- [Then sequence 50] es6 x 50,806 ops/sec ±17.34% (54 runs sampled)
- [Then sequence 50] es6Promise x 13,661 ops/sec ±17.86% (36 runs sampled)
- [Then sequence 50] bluebird x 84,802 ops/sec ±14.24% (58 runs sampled)
- [Then sequence 50] when x 337,793 ops/sec ±10.03% (63 runs sampled)
Running Benchmark Suite for test - Then sequence 100 ...
- [Then sequence 100] es6 x 27,147 ops/sec ±11.96% (54 runs sampled)
- [Then sequence 100] es6Promise x 7,078 ops/sec ±12.77% (34 runs sampled)
- [Then sequence 100] bluebird x 43,338 ops/sec ±11.36% (58 runs sampled)
- [Then sequence 100] when x 200,033 ops/sec ±8.15% (63 runs sampled)
Running Benchmark Suite for test - Then sequence 1000 ...
- [Then sequence 1000] es6 x 2,651 ops/sec ±11.70% (55 runs sampled)
- [Then sequence 1000] es6Promise x 843 ops/sec ±11.40% (48 runs sampled)
- [Then sequence 1000] bluebird x 4,851 ops/sec ±9.46% (64 runs sampled)
Fastest for Then sequence 1000 is [ 'lie' ]
Running Benchmark Suite for test - Then sequence 10000 ...
- [Then sequence 10000] es6 x 232 ops/sec ±17.72% (50 runs sampled)
- [Then sequence 10000] es6Promise x 74.30 ops/sec ±16.88% (51 runs sampled)
- [Then sequence 10000] bluebird x 443 ops/sec ±16.93% (64 runs sampled)
- [Then sequence 10000] when x 2,115 ops/sec ±9.78% (62 runs sampled)
Running Benchmark Suite for test - Then thenning 1 ...
- [Then thenning 1] es6 x 1,691,210 ops/sec ±12.31% (58 runs sampled)
- [Then thenning 1] es6Promise x 206,552 ops/sec ±15.33% (46 runs sampled)
- [Then thenning 1] bluebird x 271,149 ops/sec ±53.22% (5 runs sampled)
- [Then thenning 1] when x 1,998,426 ops/sec ±15.15% (66 runs sampled)
Running Benchmark Suite for test - Then thenning 2 ...
- [Then thenning 2] es6 x 1,662,126 ops/sec ±19.54% (55 runs sampled)
- [Then thenning 2] es6Promise x 165,546 ops/sec ±18.25% (47 runs sampled)
- [Then thenning 2] bluebird x 286,608 ops/sec ±51.44% (5 runs sampled)
- [Then thenning 2] when x 1,857,299 ops/sec ±15.54% (67 runs sampled)
Running Benchmark Suite for test - Then thenning 10 ...
- [Then thenning 10] es6 x 1,217,834 ops/sec ±13.63% (62 runs sampled)
- [Then thenning 10] es6Promise x 60,388 ops/sec ±14.14% (45 runs sampled)
- [Then thenning 10] bluebird x 130,328 ops/sec ±19.96% (12 runs sampled)
- [Then thenning 10] when x 1,076,037 ops/sec ±17.60% (64 runs sampled)
Running Benchmark Suite for test - Then thenning 50 ...
- [Then thenning 50] es6 x 453,404 ops/sec ±11.69% (64 runs sampled)
- [Then thenning 50] es6Promise x 13,114 ops/sec ±18.15% (44 runs sampled)
- [Then thenning 50] bluebird x 20,499 ops/sec ±37.27% (29 runs sampled)
- [Then thenning 50] when x 330,184 ops/sec ±14.26% (62 runs sampled)
Running Benchmark Suite for test - Then thenning 100 ...
- [Then thenning 100] es6 x 172,900 ops/sec ±19.92% (56 runs sampled)
- [Then thenning 100] es6Promise x 5,310 ops/sec ±18.34% (35 runs sampled)
- [Then thenning 100] bluebird x 11,095 ops/sec ±19.15% (48 runs sampled)
- [Then thenning 100] when x 198,358 ops/sec ±17.21% (64 runs sampled)
Running Benchmark Suite for test - Then thenning 1000 ...
- [Then thenning 1000] es6 x 24,425 ops/sec ±14.55% (64 runs sampled)
- [Then thenning 1000] es6Promise x 802 ops/sec ±20.16% (44 runs sampled)
- [Then thenning 1000] bluebird x 1,180 ops/sec ±14.13% (55 runs sampled)
- [Then thenning 1000] when x 22,541 ops/sec ±16.89% (66 runs sampled)
Running Benchmark Suite for test - Then thenning 10000 ...
- [Then thenning 10000] es6 x 1,701 ops/sec ±21.85% (53 runs sampled)
- [Then thenning 10000] es6Promise x 75.80 ops/sec ±16.52% (49 runs sampled)
- [Then thenning 10000] bluebird x 143 ops/sec ±16.35% (61 runs sampled)
- [Then thenning 10000] when x 2,044 ops/sec ±11.98% (68 runs sampled)

process.nextTick gets minified to process.r

When running npm run build-all and inspecting dist/es6-promise.min.js, I see a reference to process.r. I think the fix is to add externs: ['node'] to the the Brocfile's Closure Compiler options.

if (env === 'production') {
  trees.push(closureCompiler(moveFile(bundle, {
    srcFile: 'es6-promise.js',
    destFile: 'es6-promise.min.js'
  }), {
    compilation_level: 'ADVANCED_OPTIMIZATIONS',
    externs: ['node'],
  }));
}

Bower support

@jakearchibald I know you are only pushing dist builds to s3 rather than checking them in. Which makes bower support a little more difficult.

In the meantime, could I create a mirror of those packages on https://github.com/components/es6-promises and register es6-promises as a bower package? I have access to the components org. Its sorta the official place for unofficial bower mirrors.

I can do most of this on my own, just asking for your permission.

Chrome's promises are broken (currently)

failing test (in theory could be used in the polyfil check)

function immed(s) { x++; s(); }
function incX(){ x++; }

var x = 0;
var thenable = { then: immed };
var results = [];

for (var i=0; i<3; i++) {
  Promise.resolve(thenable).then(incX);
  results.push(x);
}

setTimeout(function(){
    results.push(x);
    console.log(results);
},100);

(cc @domenic)

Build failure

A fresh clone and install:

npm run prepublish

> [email protected] prepublish /Users/nicolasgallagher/Code/necolas/es6-promise
> ember build --environment production

version: 0.0.40
Buildingbroccoli-file-mover has been deprecated. Please use broccoli-funnel instead.
Build failed.
[{value: default, type: Literal, loc: null}, {type: FunctionDeclaration, id: [object Object], params: [object Object],[object Object], defaults: , body: [object Object], rest: null, generator: false, expression: false, returnType: undefined, parametricType: undefined, loc: [object Object]}] does not match field "arguments": [Expression | SpreadElement] of type CallExpression
AssertionError: [{value: default, type: Literal, loc: null}, {type: FunctionDeclaration, id: [object Object], params: [object Object],[object Object], defaults: , body: [object Object], rest: null, generator: false, expression: false, returnType: undefined, parametricType: undefined, loc: [object Object]}] does not match field "arguments": [Expression | SpreadElement] of type CallExpression
    at add (/project/es6-promise/node_modules/es6-module-transpiler-amd-formatter/node_modules/recast/node_modules/ast-types/lib/types.js:514:24)

Expose config

Would be useful for testing as it allows the async handler to be overridden

var Promise = require('es6-promise')

I don't know about how quite this ES6 exports syntax works, but would it be possible to set the exports up so the default is the promise constructor directly?

var Promise = require('es6-promise') 
// vs
var Promise = require('es6-promise').Promise

In regular node I'd suggest something like this:

// ...
module.exports = Promise
module.exports.polyfill = polyfill
module.exports.Promise = Promise // also support verbose syntax 

Single function exports provide a clear entry point to an API while being terse. Not sure about the downsides.

Possible?

Uncaught errors dissapearing

Here's an example:

var Promise = require('es6-promise').Promise;

Promise.resolve().then(function (x) {
    console.log("Doing something");
    throw Error("Agh, it went wrong");
}).catch(function (err) {
    cowsole.log("We had an accident: " + err.message);
});

There's no obvious hints that anything went wrong, since I mis-spelt console. Now reading through some comments on the associated article it sounds like the error will be thrown on garbage-collection, but it's not yet. As far as I can see, I can't even implement my own .catch(done) to re-throw these errors properly, since any errors within catch will be caught.

So:-

  1. Is this a bug, and at some point I can look forward to not worrying about this?
  2. Is there a way to stop errors being caught by the promise machinations?
  3. Am I being a dolt and missing something obvious?

Easier way to Polyfill in node

Perhaps out of scope of this library (maybe worth a simple light wrapper library?) but it would nice if there were a nicer way to 'polyfill' Promises in Node. Right now (unless I've made a mistake - equally likely) the only way I could see to do this was with the following line of code somewhere at the beginning of my node app:-

require('es6-promise/dist/commonjs/promise/polyfill').polyfill();

'catch' is a reserved word in IE 8 / 9

And thus, you cannot use promise.catch() if you plan on supporting IE 8 or 9.

I realize that this isn't a bug that can be fixed, but it is perhaps worth mentioning it as a limitation of this polyfill so developers aren't caught off guard when their app breaks on IE 8/9.

CommonJS issue

The library is not adopted as CommonJS module for use with browserify etc.
Probably, because of redefining such variables as exports and require.

Update to RSVP 3.0.6 - some optimizations in place

Some of the change log entries for RSVP (that this is based on) indicate optimizations being put in place. Would it be practical to update es6-promise to that? I see that they added RSVP.Promise - though I'm not certain how/if that would be a conflict or benefit.

Browser support

This polyfill looks super cool. However I couldn't find the browser support that this has. Any idea of which browsers support this implementation? It'll be really cool to have this information clearly defined in the README.

Thanks!

component.io support

It'd be nice if there was a component.json file for use with component.io. So far this is the best library for me, but it breaks my build system a little.

Possibly unhandled error should be thrown

Node v0.10, non-harmony. Such as:

var Promise = require('bluebird');
new Promise(function () { throw('err') });
// Unhandled error will be thrown.
var Promise = require('es6-promise').Promise;
new Promise(function () { throw('err') });
// Nothing will happen.

Typo in jsdoc

The parameter names in the jsdoc on then don't match the actual names, and the parameters aren't marked as optional. This causes jsdoc aware tools to raise warnings. And labelling is spelled incorrectly.

/**
  @method then
  @param {Function} onFulfilled
  @param {Function} onRejected
  @param {String} label optional string for labeling the promise.
  Useful for tooling.
  @return {Promise}
*/
  then: function(onFulfillment, onRejection, label) {

asap does not work in older versions of IE when browserified

The code in question can be found here: https://github.com/jakearchibald/es6-promise/blob/a6e3474aa757d47bcac38eaf303645fba59c5278/lib/promise/asap.js#L3

As near as I can tell, this code assumes one of 3 things:

  1. global is present
  2. "this" keyword is undefined
  3. "this" keyword is the global object

However, if this library is included in a web application via browserify then it's possible for global to not be present and "this" equal to the exports object (since browserify mimics node unlike requirejs). You wind up in a situation where on older versions of IE, local is actually the exports object and it fails when using the setTimeout method.

Not sure if you consider this to be a downstream problem but figured I'd report it here first.

Node domains and timing

I am experiencing some issues around node domains and the timing of when then is called on a Promise.

Basically, all continuations created before the Promise is resolved loose their domain, and everything created after the Promise is resolved stay in the same domain.

var Promise = require('es6-promise').Promise;
var request = require('request');

var p = new Promise(function(resolve, reject) {
  request('http://www.google.com', function(err, response, body) {
    console.log('done loading google.com')
    resolve(response.statusCode);
  });
});

var domain = require('domain');

var x = 0;

var f = function() {
  var id = ++x;
  console.log('%d waiting',id);

  var d = domain.create();

  process.domain = d;
  process.domain.x = 1;
  p.then(function(result) {
    console.log('%d continues',id);
    console.log('result = %d', result); 
    console.log('process.domain? %s', !! process.domain);
  });
};

f();
process.nextTick(f);
setTimeout(f, 100);
setTimeout(f, 1000);

Output:

1 waiting
2 waiting
3 waiting
done loading google.com
1 continues
result = 200
process.domain? false
2 continues
result = 200
process.domain? false
3 continues
result = 200
process.domain? false
4 waiting
4 continues
result = 200
process.domain? true

no method 'all'

Using require( 'es6-promise' ).polyfill()
in nodejs v0.10.30 I get
TypeError: Object #<$$es6$promise$promise$$Promise> has no method 'all'

IE10 Failure

I've integrated es6-promises into Igaro App and found an issue with IE10 which I've been unable to squash. I believe this describes the pattern;

A promise runs an XHR which requests a file and from it loads another promise which requests further files. When these files are fetched the promise completes but doesn't exec the original callback and the first promise thus hangs.

It's fine on Safari, Firefox and Chrome. I may have done a poor job explaining it so hopefully the code will help.

https://github.com/igaro/igaro/blob/master/3/compile/app/js/core.amd.js

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.