Angular 在原始函数之前的 then 子句中触发函数

Angular firing function in then clause before original function

我有一个可以同时调用的服务调用列表,但我还有 1 个必须调用的调用,并且必须在其他调用被触发之前完成。我对此进行了设置,因此直到调用的 .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 调用被触发之前被调用,为什么?

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

如果您只想在 partnerAddRepository.addExisting 具有 return 值后调用其他函数,那么您应该在 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);
});