Home >Web Front-end >JS Tutorial >How Can I Properly Use $.when() with an Array of Deferred Objects?
Incorporating an Array of Deferred Objects into $.when()
In certain scenarios, passing an array of Deferred objects to $.when() may be necessary, but an issue arises when $.when() struggles to recognize the array as a Deferred object, resulting in premature execution.
Solution:
To resolve this issue, utilize Function.prototype.apply to pass the array to $.when():
$.when.apply($, my_array).then(function() { ... });
ES6 Spread Operator:
In ES6 onwards, the spread operator ( ... ) offers an alternative:
$.when(...my_array).then(function() { ... });
Argument Handling:
Since the number of required parameters for the .then handler may be uncertain, the handler must process the arguments array to obtain the results of each promise.
The above is the detailed content of How Can I Properly Use $.when() with an Array of Deferred Objects?. For more information, please follow other related articles on the PHP Chinese website!