How do you work with an array of jQuery Deferreds?

前端 未结 4 1156
余生分开走
余生分开走 2020-11-22 15:02

I have an application that requires data be loaded in a certain order: the root URL, then the schemas, then finally initialize the application with the schemas and urls for

4条回答
  •  梦谈多话
    2020-11-22 15:47

    extends when with this code:

    var rawWhen = $.when
    $.when = function(promise) {
        if ($.isArray(promise)) {
            var dfd = new jQuery.Deferred()
            rawWhen.apply($, promise).done(function() {
                dfd.resolve(Array.prototype.slice.call(arguments))
            }).fail(function() {
                dfd.reject(Array.prototype.slice.call(arguments))
            })
            return dfd.promise()
        } else {
            return rawWhen.apply($, arguments)
        }
    }
    

提交回复
热议问题