Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise.all() work around for IE 11 with Backbone

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?

like image 475
Denoteone Avatar asked Dec 13 '16 21:12

Denoteone


People also ask

Do promises work in IE11?

IE11 neither supports arrow functions nor native Promises. Use a JS transpiler (like babel) or don't use ES6 features. For Promise support you can use a library like bluebird.

Is promise supported in IE?

'Promise' is undefined in Internet Explorer (IE 11)

What is the use of promise all ()?

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises.

How promise all works parallel?

Promises cannot "be executed". They start their task when they are being created - they represent the results only - and you are executing everything in parallel even before passing them to Promise. all .


2 Answers

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.

like image 122
Emile Bergeron Avatar answered Oct 19 '22 23:10

Emile Bergeron


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

like image 27
sidanmor Avatar answered Oct 19 '22 23:10

sidanmor