Within my project, there exists a scenario where various functions with differing arguments are present. Among these arguments is one that contains a callback function, while some functions have no arguments at all. The examples below illustrate this:
abc(string, function, number)
aaa(function, string)
bcd()
xyz(function)
cda(string, number, function, string)
I am in need of writing a function that can handle any irregularity in the above functions and always return a promise.
For instance:
// Assuming $q has been injected
var defer = $q.defer();
function returnPromise(functionName, functionArgumentsInArray){
defer.resolve(window[functionName].apply(null, functionArgumentsInArray));
return defer.promise;
}
It is important to note that in the example function above, only the main function is resolved and not the callback function within it.
Note: Each function will have a maximum of one function argument or none at all.
While it is possible to handle each case individually like so:
function returnPromise(){
var defer = $q.defer();
abc("hey", function() {
defer.resolve(arguments);
}, 123);
return defer.promise;
}
I am actually seeking a universal wrapper that can encompass all such functions.