Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

javascript - Are there promise standards for extended functions like .all(), .finally(), etc

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...