Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链式jQuery.then()-下一个是否仅在上一次解析完成后执行_Javascript_Jquery_Firefox_Promise_Jquery Deferred - Fatal编程技术网

Javascript 链式jQuery.then()-下一个是否仅在上一次解析完成后执行

Javascript 链式jQuery.then()-下一个是否仅在上一次解析完成后执行,javascript,jquery,firefox,promise,jquery-deferred,Javascript,Jquery,Firefox,Promise,Jquery Deferred,我有这个密码 $.when( // I checked the first one and assume all return promises datacontext.entity1.getPromise({ ...options... }), datacontext.entity2.getPromise({ ...options... }), datacontext.entity3.getPromise({ ...options... }), data

我有这个密码

$.when(
    // I checked the first one and assume all return promises
    datacontext.entity1.getPromise({ ...options... }),
    datacontext.entity2.getPromise({ ...options... }),
    datacontext.entity3.getPromise({ ...options... }),
    datacontext.entity4.getPromise({ ...options... }),
    datacontext.entity5.getPromise({ ...options... }),
    datacontext.entity6.getPromise({ ...options... }),
    datacontext.entity7.getPromise({ ...options... }),                    )
.then(
    // some more similar calls returning promises
    datacontext.entity8.getPromise({ ...options... }),
    datacontext.entity9.getPromise({ ...options... }),
    datacontext.entity10.getPromise({ ...options... }),
    datacontext.entity11.getPromise({ ...options... }),                    
)
.then(function() {
    // do work with the results
});
如果我理解正确,在解决
when()
中的所有延迟之前,不应开始执行first
then()
。对吗? 我是否应该对两个
then()
部分期望相同的结果,即
//do work with the results
在前面的
then()
的所有调用都已解决之前,不应到达
?这在Chrome中似乎是正确的,但在FF中,我在解决所有延迟之前点击了最后一个
then()

那么根据评论,这是连锁的正确方式吗

$.when(
    // I checked the first one and assume all return promises
    datacontext.entity1.getPromise({ ...options... }),
    datacontext.entity2.getPromise({ ...options... }),
    datacontext.entity3.getPromise({ ...options... }),
    datacontext.entity4.getPromise({ ...options... }),
    datacontext.entity5.getPromise({ ...options... }),
    datacontext.entity6.getPromise({ ...options... }),
    datacontext.entity7.getPromise({ ...options... })
)
.then(function() {
    $.when(
        // some more similar calls returning promises
        datacontext.entity8.getPromise({ ...options... }),
        datacontext.entity9.getPromise({ ...options... }),
        datacontext.entity10.getPromise({ ...options... }),
        datacontext.entity11.getPromise({ ...options... })
    )
    .then(function() {
        // do work with the results
    });
})

在jQuery中,
$.when
(和标准中的
Promise.all
)获取承诺列表,并返回一个承诺,该承诺在所有承诺解决时解决

.then
方法表示继续,并接受满足和拒绝处理程序。当您从
返回承诺时,
仅当返回的承诺已解决时,该承诺才会继续。当您返回一个值时,它将“立即”继续

因此,您要做的是返回一个
$。当从第二个处理程序返回
时,然后:

$.when(
    // I checked the first one and assume all return promises
    datacontext.entity1.getPromise({ ...options... }),
    datacontext.entity2.getPromise({ ...options... }),
    datacontext.entity3.getPromise({ ...options... }),
    datacontext.entity4.getPromise({ ...options... }),
    datacontext.entity5.getPromise({ ...options... }),
    datacontext.entity6.getPromise({ ...options... }),
    datacontext.entity7.getPromise({ ...options... }))
.then(function(){ // our handler, note that its params are the resolution values

    return $.when( // which will resolve when this composite promise will resolve
    // some more similar calls returning promises
    datacontext.entity8.getPromise({ ...options... }),
    datacontext.entity9.getPromise({ ...options... }),
    datacontext.entity10.getPromise({ ...options... }),
    datacontext.entity11.getPromise({ ...options... }),                    
    )
})
.then(function() {
    // do work with the results
});
请允许我建议:

  • 使用真正的promise实现,jQuery延迟对象非常有限
  • 使用返回值而不是使用全局/闭包变量从promise传递数据

  • 。然后
    接受两个函数,而不是承诺列表。您必须传递一个函数,在函数内部调用
    ,将承诺传递给它,并在
    中返回返回值。请查看
    的文档,然后查看