27

I wanna know whether JS promises were a part of ES5? If so, why it doesn't work sometimes in the older browsers and we have to add a polyfill for them. Also, which polyfill should be added in that case, an ES5 one or ES6? I have a little confusion regarding that.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Param Singh
  • 1,325
  • 3
  • 13
  • 28
  • 1
    ES5 did not have promises. Libraries like jQuery or Angular had their own proprietary promise implementations. – jfriend00 Jul 17 '16 at 18:25
  • ES5 does not have promises as part of the language. For ES5, promises are just a design pattern. There are indeed many libraries in ES5 that implements many different types of promises. You can even implement them yourself since it's just a design pattern. – slebetman Jul 17 '16 at 18:26
  • @slebetman Can you provide me with any easy example for the promise implementation? – Param Singh Jul 17 '16 at 18:32
  • @ParamSingh - There are already two promise libraries for use with ES5 in my answer. – jfriend00 Jul 17 '16 at 18:37

1 Answers1

47

ES5 did not have promises. Libraries like jQuery or Angular had their own custom and non-standard promise implementations.

Popular Promise implementations for use with ES5 are Bluebird (which is compatible with the ES6 standard) and Q (which was not originally compatible with the ES6 standard- though seems to be moving that direction) and RSVP.

Neither are actual polyfills in that they don't get out of the way if native promises are present because they add additional features.

There are some pure polyfills for ES6 promises. Here's one that is a subset of RSVP: https://github.com/stefanpenner/es6-promise.

There are reasons to use more than a polyfill as you can read here: Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises?

Asking for a recommendation of a specific polyfill library is considered off-topic here on Stack overflow so I won't comment on that.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @KirillSlatin - I already changed to "custom and non-standard", though I'd rather be spending energy on meaningful content rather than wordsmithing something won't be confusing to anyone. – jfriend00 Jul 17 '16 at 18:36
  • @jfriend00 Actually, the codepen has a title "ES5 Promises" which made me wonder about whether es5 had promises too! – Param Singh Jul 17 '16 at 18:37
  • 1
    @ParamSingh See http://stackoverflow.com/questions/23772801/basic-javascript-promise-implementation-attempt , http://stackoverflow.com/questions/36192728/understanding-the-promises-a-specification – guest271314 Jul 17 '16 at 18:40
  • 3
    @ParamSingh - ES5, the standard, does not include promises. We've already said that. Browsers have, for a long time moved beyond ES5 and been implementing parts of ES6, including promises. But, a strict ES5 implementation does not have promises and they are not part of the ES5 standard in any way. They were added to the standard in ES6. There is a separate Promises/A+ standard that defines just promises that has existed longer than ES6. – jfriend00 Jul 17 '16 at 18:40
  • @jfriend00 , yeah while you have raised a point on Promises/A+, can you please explain a little about it. I've come across this standard while reading some tutorials on internet. What is this standard? Is it a part of ES? And since when it's been existing? – Param Singh Jul 17 '16 at 18:49
  • 1
    @ParamSingh - [Promises/A+](https://promisesaplus.com/) defines the behavior of `.then()` handlers for promises which is one element of an overall promise standard. ES6 incorporated this part of the promise definition in their specification. – jfriend00 Jul 17 '16 at 18:51