2

Are there standards or emerging standards for extended promise functions such as .all(), .finally(), .catch(), .spread(), .settle(), etc...

I know about the Promises A+ spec, but that only appears to deal with .then() and I can't find standards for all the other useful functions. I'm familiar with jQuery, Q and Bluebird and there are meaningful differences among all which seems like it should be a temporary condition as everything should converge to a standard over time since there's really no reason to have different names for similar pieces of functionality. I know jQuery isn't even fully Promises A+ compatible, but apparently it is a stated goal to at least move to that, but what about all the other useful functions?

My motivation for wanting to understand what the current and future standards development looks like is to know which current functions offered in the various libraries are more likely to be consistent with future standards and which are not so I can write code that will need less maintenance in this area in the future. I've done a bunch of searching and it seems to be a hard thing to find (for me anyway). I can see Promise.all() in an ES6 draft spec, but don't see any of the others.

Are there proposed standards for advanced promise functions such as .all(), .finally(), .catch(), .spread() and .settle()?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    ES6 has some of those on deck... – dandavis Dec 03 '14 at 23:56
  • 1
    Why downvotes on the question? Seems like a legit software question with a factual answer possible. If I'm going to do development using these extended promise features, I'd like to select a library that is as close to any emerging standard as possible so there would be the least rewrite or adaptation in the future. – jfriend00 Dec 04 '14 at 00:50
  • @dandavis - can you point me to an ES6 draft that has some of these in it. I looked [here](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-promise-prototype-object) and only see `Promise.all()`. – jfriend00 Dec 04 '14 at 00:51
  • 1
    I found this [Oct. 14, 2014 draft of the ES6 harmony specification](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts) that contains `Promise.all()` and `Promise.race()`, but none of the other helpful methods. I'm wondering if, like the Promises A+ specification are there other Promise specifications outside of javascript specs that are leading the development of Promise standards? – jfriend00 Dec 04 '14 at 01:29

1 Answers1

6

I know about the Promises A+ spec, but that only appears to deal with .then()

Yes. The aim of the Promises/A+ spec is to "detail the behavior of the then method, providing an interoperable base which all Promises/A+ conformant promise implementations can be depended on to provide" and also describes promise assimilation, i.e. how to convert a promise-like object to a "real" Promise of your own library. In one word, it focuses on interoperability by speccing the minimum interface.

I can't find standards for all the other useful functions.

Yes. Every library does define its own. "Standards" emerge when a particular feature is present in (and copied to) many implementations.

My motivation for wanting to understand what the current and future standards development looks like is to know which current functions offered in the various libraries are more likely to be consistent with future standards and which are not so I can write code that will need less maintenance in this area in the future.

Probably watching the issue discussions of the big implementations is the best idea. However, most of the offered features are relatively easy to polyfill.

The gold standard will be ES6 and ES7. Check the esdiscuss mailing list regularly for discussion of features, questions on the usage, and new drafts.

I can see Promise.all() in an ES6 draft spec, but don't see any of the others.

If you look more closely, the Promise section details:

  • The Promise constructor, Promise.reject and Promise.resolve
  • Promise.all and Promise.race
  • Promise.prototype.catch and Promise.prototype.then

.finally(), .catch(), .spread(), .settle()

  • finally was discussed on esdiscuss (e.g. here), but probably won't make it into ES6.
  • see above for catch
  • spread is superseded by the spread operator and destructuring
  • settle could be implemented using all, or at least using an algorithm very close to the all spec
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    @upvoter: What's going on here? One upvote in 35 seconds? Did you even have time to read the post? :-) – Bergi Dec 04 '14 at 02:22
  • 1
    From this, I conclude that it will be hard to do any serious development using only the promises in ES6. Additional promise features will be needed. `.finally()` is fairly trivial to polyfill I guess, but I regularly find a need for `.settle()` which isn't trivial to implement (IMO way more useful than `.race()`). I guess we just keep using 3rd party Promise libraries then. Too bad more of it isn't on a standards track so at least the 3rd parties are converging ahead of ES7. – jfriend00 Dec 04 '14 at 02:29
  • Wouldn't `settle` be "just" `Promise.all(promises.map(p => p.then(v => {value:v, state:"fulfilled"}, e => {reason:e, state:"rejected"})))`? I think all of these functions are already on the standards track, they only have not yet proven useful enough or had a stable interface to be shipped with ES6. – Bergi Dec 04 '14 at 02:36
  • My understanding is that `.all()` gets rejected immediately when any promise in the bunch is rejected even before other promises in the bunch have finished. Sorry, but I don't understand the notation you're using. Can you point me to at least standards discussions for these other functions. I care more about not having to rework code in the future (e.g. coding to use standards-track stuff), not so much about exactly what is in ES6 as I will be using a 3rd party promise library for awhile anyway. – jfriend00 Dec 04 '14 at 02:37
  • @jfriend00: They're ES6 arrow functions, shorter than function expressions but with similar usage (think: lambda). All errors in the promises get handled by the second `then` callback, `all` would only see fulfilled promises. – Bergi Dec 04 '14 at 02:40