Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Last active December 20, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/295cf2f00953e061d49b to your computer and use it in GitHub Desktop.
Save juandopazo/295cf2f00953e061d49b to your computer and use it in GitHub Desktop.
Promises status

Promises status

done() and debugging

Arguments for done()

  • then() is to map() as done() is to forEach()
  • Error handling

Arguments against

  • Now you have to teach when to use each method
  • No clear consensus in A+
  • It doesn't look like DOM Promises will include it at the moment

Alternatives

One option is to throw/log when a promise is rejected and no rejection callbacks were added to it. Throwing doesn't sound like a good idea because of Promise.reject(foo). We could make use of the debug filter in YUI. Another option is to do something a lot more complicated and try to do static analysis to figure out if an error ir never handled.

To polyfill or not to polyfill

  • DOM Promises can't be subclassed. In the future we'll possibly (probably?) have to do:
let resolver = Symbol();

class MyPromise extends Promise {
  static [@@create]() {
    var promise = new Promise(r => this[resolver] = r);
    promise.__proto__ = MyPromise.prototype;
    return promise;
  }
  constructor(...args) {
    // initialize `this`
  }
}
  • Subclassing looks a bit like an anti-pattern. AbortableProgressPromise. It's possible we want promises to be a behavior of an object, not a type. Composition over inheritance

Changes in ES6

There's been a lot of discussion about monads vs promises. Promises are almost monads. ES6 will probably introduce two changes:

  • flatMap a method that does satisfy to the monad laws (allows promises for promises)
  • Promises will have a new accepted state when resolved to other promises

This conflicts with a change in the Pull Request that treats promises different from thenables.

Performance improvements

Performance analysis show no dramatic gains. Synchronous resolution is something we should add at some point, the DOM does it.

Decisions to make

  • Include done or not include it
  • Work on a polyfill or not
  • Notify on unhandled rejected promises
  • Drop the pull request and start a new one with safe changes (only API additions in DOM, performance improvements)
@derek
Copy link

derek commented Aug 8, 2013

Can we link to this for the show notes & YUI Weekly? It's marked secret, so just wanted to double-check. Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment