Pass in an array of Deferreds to $.when()

后端 未结 9 1244
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 22:20

Here\'s an contrived example of what\'s going on: http://jsfiddle.net/adamjford/YNGcm/20/

HTML:

Click me!
&l
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 22:52

    To pass an array of values to any function that normally expects them to be separate parameters, use Function.prototype.apply, so in this case you need:

    $.when.apply($, my_array).then( ___ );
    

    See http://jsfiddle.net/YNGcm/21/

    In ES6, you can use the ... spread operator instead:

    $.when(...my_array).then( ___ );
    

    In either case, since it's unlikely that you'll known in advance how many formal parameters the .then handler will require, that handler would need to process the arguments array in order to retrieve the result of each promise.

提交回复
热议问题