10

Background: I have been tasked to help resolve an issue with the following error:

'Promise' is undefined'

Which is part of our sessionsmodel.js script:

return Promise.all(promises);

promises is an array of actions that need to happen and if any fail it is rejected.

Question: Based on my research IE does not support Promise so is there a work around that can be applied to this return value that will accomplish the same thing?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Denoteone
  • 4,043
  • 21
  • 96
  • 150

3 Answers3

7

Since you are using Backbone, the promises are probably jQuery promises. You could use jQuery .when function to do the same as Promise.all:

return $.when.apply($, promises);

For most other simple situations where you call functions like save and fetch, you could avoid promises completely by using the provided callbacks:

model.save({
    context: this,
    success: this.onModelSuccess
});

There's no need to use another library because Backbone uses jQuery already (by default), unless you don't like jQuery deferred or that you're using something else in place of jQuery.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • For anyone still supporting IE11, and happen to use jQuery < 3, note that if `promises` holds native promises instead of `$.Deferred` objects, they won't be unwrapped, [see this answer](https://stackoverflow.com/a/51510862/3002584). – OfirD Nov 13 '22 at 15:46
1

ES6 Promise spec was implemented by "good" libraries like Q, When, RSVP, Bluebird, Lie and more...

If you want to learn more on Promises, check this link: Promises

sidanmor
  • 5,079
  • 3
  • 23
  • 31
1

I recommend you use a polyfill.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331