Ich versuche, eine variable Anzahl von Funktionen in Q.all()ein dynamisches Array von Funktionen für Q.all Gebäude() in JScript
Es funktioniert gut zu passieren, wenn ich manuell die Array-Code - jedoch Ich möchte es in einer Schleife aufbauen, da das System nicht weiß, wie oft die Funktion bis zur Laufzeit aufgerufen wird - und für jeden AJAX-Aufruf eine andere ID eingeben muss.
Ich habe verschiedene Methoden ohne Erfolg ausprobiert (z. B. array[i] = function() {func}
) - ich denke, eval()
könnte ein letzter Ausweg sein.
Jede Hilfe wäre massiv hilfreich.
// 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();
Vielen Dank im Voraus.
Wie sieht die erfolgreiche manuelle Version aus? –
Sicher, Q.all() akzeptiert eine Reihe von Versprechen, nicht ein Array von Funktionen. –