Overview of How To Use Promises


I’m looking for some help understanding on a macroscopic level how I should be using promises in my Emberjs/EmberData app. Can anyone give me a summary of the important things to know about promises?

Specifically, I’m looking for the anwers to the following kinds of questions:

  • What parts of my app should be dealing with promises (i.e. calling then(), creating promises, creating promise array etc.)? Do I deal with promises just in my router? Should my controller code be dealing with promises as well? Should my model code be dealing with promises? Am I correct in saying that it’s expected for my templates to use “each” on a promise array?

  • How do I know what properties should be Promises/PromiseArrays or Arrays? Do I need to check through each property on every one of my models and controllers and decide whether it ever be loaded asynchronously? What about properties that are loaded asynchronously some of the time but are set with data from the client the rest of the time? Should I make sure that those properties are always promises and create trivial promises when the data is already on the client?

  • Why are there so many Array classes? I have javascript arrays, Array, Enumerable, ArrayProxy, PromiseArray, and promises that resolve to each of the other 5. That’s a total of 6 (or 10 depending on how you count) different kinds of arrays. Should I be using all of them? Should I try to stick to just 1 or 2 of them? Which of those kinds of arrays do you use and for which purposes? Why do some kinds of arrays not support the operations I want them to support? (Like… how am I supposed to form a PromiseArray which is the concatenation of two other PromiseArrays? Is that easy? Should I even be doing things like that?)