【问题标题】:Angular firing function in then clause before original function原始函数之前的 then 子句中的角度触发函数
【发布时间】:2016-10-25 20:01:29
【问题描述】:

我有一个可以同时调用的服务调用列表,但我还有一个必须调用的其他调用,并且在其他调用被触发之前完成。我进行了设置,因此直到调用的.then(function() {}) 块才发生其他调用。检查 Chrome 开发工具(并根据 Sql 错误获得确认),then 子句中的所有调用都在之前触发。我在这里做错了什么?

        var promises = [];

        if (this.partner.customerId > 0) {
            if (this.isDirty('ipn.individualPartnerName')) {
                promises.push(this.partnerEditService.updateIndividualName(<Interfaces.IIndividualPartner>this.partner));
            }

            if (this.isDirty('bpa.mailingAddressForm') || this.isDirty('bpa.streetAddressForm')) {
                promises.push(this.partnerEditService.updateAddresses(this.partner));
            }

            if (this.isDirty('bn.businessName')) {
                promises.push(this.partnerEditService.updateBusinessName(<Interfaces.IBusinessPartner>this.partner));
            }

            if (this.isDirty('rc.individualPartnerResponsibilities') || this.isDirty('rc.businessPartnerResponsibilities')) {
                promises.push(this.partnerEditService.updateResponsibilities(this.operation, this.partner));
            }
        }

        this.partnerAddRepository.addExisting(this.operation.operationId, this.partner.customerId)
            .then(() => {
                this.executeSaves(promises);
            });


    executeSaves = (promises) => {
        this.$q.all(promises)
            .finally(() => {
                this.$mdDialog.hide(this.partner);
            });
    }

这里是 partnerAddRepo.addExisting 函数:

    addExisting = (operationId: number, partnerId: number) => {
        return this.$http.put(`my/path/to/operation/${operationId}/partner/${partnerId}`);
    };

那么,executeSaves 中的内容(包含 4 个不同的服务调用)在partnerAddRepository.addExisting 调用被触发之前被调用,为什么?

【问题讨论】:

  • 呃,请注意解释反对意见。
  • 可能是时机?您正在将这些承诺推入数组,但同时也会触发返回承诺的任何内容。

标签: javascript angularjs typescript angular-promise


【解决方案1】:

您的服务调用会在您调用它们时立即执行。 Promise 延迟函数调用的返回值,而不是函数的执行。

如果您只想在partnerAddRepository.addExisting 返回值后调用另一个函数,那么您应该在then 回调中创建承诺数组。

this.partnerAddRepository.addExisting(this.operation.operationId, this.partner.customerId)
    .then(() => {
        var promises = [];

        if (this.partner.customerId > 0) {
            if (this.isDirty('ipn.individualPartnerName')) {
                promises.push(this.partnerEditService.updateIndividualName(<Interfaces.IIndividualPartner>this.partner));
            }

            if (this.isDirty('bpa.mailingAddressForm') || this.isDirty('bpa.streetAddressForm')) {
                promises.push(this.partnerEditService.updateAddresses(this.partner));
            }

            if (this.isDirty('bn.businessName')) {
                promises.push(this.partnerEditService.updateBusinessName(<Interfaces.IBusinessPartner>this.partner));
            }

            if (this.isDirty('rc.individualPartnerResponsibilities') || this.isDirty('rc.businessPartnerResponsibilities')) {
                promises.push(this.partnerEditService.updateResponsibilities(this.operation, this.partner));
            }
        }

        this.executeSaves(promises);
});

【讨论】:

  • 完美的先生,谢谢。从来没有想过我需要将整个块嵌套在 .then()
  • 我可能会将该块放入一个函数中,并且只调用该函数。这样then 就不那么乱了
猜你喜欢
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多