I am trying to pass a variable number of functions to Q.all ()
It works fine if I encode the array manually - however, I want to create it in a loop, since the system will not know how many times to call the function before execution - and it needs to pass a different identifier to it for each AJAX call.
I tried various methods without success (for example, array[i] = function() {func} ). I think eval() may be the last.
Any help would be very helpful.
// Obviously this array loop wont work as it just executes the functions in the loop // but the idea is to build up an array of functions to pass into Q var arrayOfFunctions = []; for(var i in NumberOfPets) { arrayOfFunctions[i] = UpdatePets(i); } // Execute sequence of Ajax calls Q.try(CreatePolicy) .then(updateCustomer) .then(function() { // This doesn't work - Q just ignores it return Q.all(arrayOfFunctions) // This code below works fine (waits for all pets to be updated) - I am passing in the ID of the pet to be updated // - But how can I create and pass in a dynamic array of functions to achieve this? // return Q.all([UpdatePets(1), UpdatePets(2), UpdatePets(3), UpdatePets(4), UpdatePets(5), UpdatePets(5)]); }) .then(function() { // do something }) .catch(function (error) { // error handling }) .done();
Thanks in advance.
source share