I would like to implement dependency calls of Deferred/Promise. The problem is I don't understand how to make calls step-by-step for array of handlers that contains async calls e.g.
I have global object :
var options = {
additionalHandler: []
main : null
}
Then add main function and handlers that add in strong sequential and also need to call in this sequences
options.main = function(that, data){
var that = this; //Todo: fix this should be button
//Do some staff
}
var dialog = $(somedialog).dialog({
autoOpen: false,
modal: true,
buttons: {
"OK": function() {
//There is the place where i have wait user answer to invoke innerDeferred.resolve(true) that is have to affected on global Deferred
$(this).dialog("close");
}
},
close: function () {
//innerDeferred.resolve(false);
}
});
options.additionalHandler.push(function(){
dialog.dialog("open");
})
Than when button click happen i run/wait all handlers and then execute main function if all of handlers returns true
$("#someButton").on("click", function () {
var self = this;
if(typeof options !== "undefined"){
var deferred = $.Deferred();
if (options.additionalHandler && options.additionalHandler.length) // if we do not have(collect) any handlers we don't ran it all
{
$(options.additionalHandler).each(function () {
if (typeof this !== "undefined" && typeof this === "function") {
deferred.then(this( /*??? deferred or innerDeferred */));
}
});
}
deferred.done(function (def) {
if(def) // if all steps from all inner steps return true
options.main(self , someData));
});
}
}
Which means I have one main handler that depend on additional handler answers that possible not existed (if not existed we just go to main handler), and the questions is how implement dependency on async inner deferred call result which is should run step-by-step(dialog box in our case wait user answer and go to another dialog box or possible ajax calls).
Update1:
From the top up to bottom this is pseudo implementation(code) and also not working
How this works(required):
user => add some itemp to backet
backet <= knows about some discount if user purchase 2 items and register new pop-up that says if you add 2 you'll have 50% sale.
user => add another item to backet
backet <= this item will not available till next week, register another pop-up.
user = > push Order
Order => (pop-up) Discount if 2 do you (Yes-break /No -continue) => Item available on next week will wait ( Yes - continue/ No- break) => if (all.Continue) => push Order.
all works as waterfall, and I don't see the pros/cons with deferred/promise