Create all your promises that you know you need. Create .when with it. Keep the promise returned with .when . When you add new events that require new promises, add new .when s, using the promise of previous .when s, as well as all new promises that you have completed.
Your application will have several single points of failure if you use the final .when as your "Go ahead with the application." IE: if any promise fails at any moment, then any .when created after that .when also fail.
... but if it is your intention or you have a clear error handling, it should do you.
I'm trying to preserve this agnostic library - usually, I use my own implementation, which is halfway between something that jQuery does and something that Crockford did in a recent conversation, but if we assume that:
functions return promise handlers
"when" returns the promise handler, the monkey handlers have at least .done and .fail - either accepting two arguments or something else and everything that happens inside the function will determine whether the promise is rejected/resolved or kept/broken ( or something else), then you can get a bunch of functionality that looks like this:
var doing = doSomething(), // returns promise making = makeSomething(), // returns promise loading = loadSomething(), // returns promise dependencies_lvl_1 = when(doing, making, loading);
You can add a new module or widget later - perhaps this will save some work:
var saving = saveSomething(),
You may need to switch pages after this, but you need your cache data first
var caching = cacheData(),
If you look at this, you know that as long as when returns a promise that is successful only when all promises are saved (and when all promises are saved) and none of them break, then dependencies_lvl_2 there includes all the dependencies dependencies_lvl_1 , plus additional promises.
Level-3 .when , therefore, has permission depending on each thing that has been added to the chain.
And while you continue to cache your promises into variables (or some future access), you can continue to bind them together, for eternity.