I have a async function that breaks it's callbacks into an object success and error, this function takes one param (besides the callback) "pink".
async("pink",{
success:function(){
},
error:function(){
}
});
I want to make plural version of this function that takes in an array and returns the true or false value for the async action.
asyncs(["blue","red","green"],function(values){
console.log(values); // [true,true,true];
});
The trick is that each acync action needs to be within the next, the value of the function (true or false) needs to be pushed() into a "global" (higher in scope) variable values and the returned in a master callback(values) at the end (when the count reaches the array length)
This is a very rudimentary way of nesting each async() function and returning the values, it is limited because it manually trails only 3 array values.
var asyncs = function(params,mstrCB){
var length = params.length;
var values = [];
async(param[0],{
success:function(){
values.push(true);
async(param[1],{
success:function(){
values.push(true);
async(param[2],{
success:function(){
values.push(true);
mstrCB(values);
},
error:function(){
values.push(false);
mstrCB(values);
}
});
},
error:function(){
values.push(false);
mstrCB(values);
}
});
},
error:function(){
values.push(false);
mstrCB(values);
}
});
};