简体   繁体   English

jQuery在等待promise解析时无法获得$ .when工作

[英]jquery cannot get $.when to work when waiting for promises to resolve

I have a situation that is driving me crazy because I can't seem to get it to play out properly. 我的情况使我发疯,因为我似乎无法正常发挥作用。 I have a situation where there options to fill out a total of 3 forms. 我遇到的情况是可以选择总共填写3个表格。 However, 2 don't always ned to be filled out, only one has to be filled out. 但是,并非总是需要填写2个,只需要填写一个即可。 The problem is the main form that has to be filled out needs to wait if either of the other forms is filled because it needs to store their Id's as a lookup in the main form before saving. 问题是,如果其他任何一个表单都已填写,则必须填写的主要表单需要等待,因为它需要在保存之前将其ID作为查询存储在主要表单中。 This should be relatively simple, but I have tried pretty much everything and cannot get $.when to work properly to wait for the promises to resolve. 这应该是相对简单的,但是我已经尝试了几乎所有内容,并且在正常工作以等待承诺解决时无法获得$.。

I have a service where I call a function postItem, which is an $ajax post call 我有一个服务,我在其中调用函数postItem,这是一个$ ajax发布调用

I am following this exactly, but it doesn't work: 我正完全按照此步骤操作,但不起作用:

https://dzone.com/articles/promises-and-deferred-objects https://dzone.com/articles/promises-and-deferred-objects

var promise1, promise2;
var promise_array = [];

if(vm.receivables) {
 promise1 = user.postDealItem(args....).then(function(response) {
           vm.model.recId = response.Id;
           return response
});
}

if(vm.standard) {
 promise2 = user.postDealItem(args...).then(function(response) {
           vm.model.stdId = response.Id;
           return response
});
}

if(promise1 != null) promise_array.push(promise1);
if(promise2 != null) promise_array.push(promise2);

$.when.apply($,promise_array).done(function(response) {
    //never gets to this point
});

Before the other 2 forms were needed, I had a single form which would complete and then once that completed I had several other sublists that were saved and it worked without issue by chaining the promises... 在需要其他两种表单之前,我只有一个表单可以完成,然后完成后,我保存了其他几个子列表,并且通过链接诺言实现了工作而没有问题...

Now it goes past the $.when part of the code, hits the end of the block and then goes into a bunch of angular functions, and then tracedigest++ and then somewhere in there it errors out and reloads the main page. 现在,它超出了代码$ .when的部分,到达了代码块的末尾,然后进入了一系列的角度函数,然后进入了tracedigest ++,然后在该处的某个地方出错并重新加载了主页。 I am testing this only running one other form and that initial form does save properly, the promise just doesn't resolve properly... 我正在测试仅运行另一种形式,并且初始形式可以正确保存,但诺言无法正确解决...

Any help would be greatly appreciated, I am at my wits end as to why this isn't working as it appears it should be fine the way it is... 任何帮助将不胜感激,我不知所措,为什么它不起作用,因为它看起来应该是很好的方式...

It looks to me like you're just missing the () after postDealItem. 在我看来,您只是在postDealItem之后缺少() Change the two places where it says user.postDealItem.then(function(response) { to user.postDealItem().then(function(response) { . 将显示user.postDealItem.then(function(response) {的两个位置更改为user.postDealItem().then(function(response) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM