是否可以在Angular 1.7中创建新的Promise对象?

问题描述 投票:0回答:1

我有一个当前使用Angular 1.7的应用程序,我们有一个IHTTPPromise(updateCase),在解析后,我们在其中处理then()方法上的代码

在第一个then()之前,我想链接另一个then()以设置x毫秒的等待时间

我不能只使用setTimeout(),因为那样就不会返回Promise。

在Angular 1.7中,我如何创建一个新的Promise来包含setTimeout,然后解析并允许我链接then语句?

this.caseService.updateCase(updateCaseRequest)
//***WANT TO ADD A WAIT HERE
    .then(
    response => {
        this.showUpdateComplete();
        this.getCases();
        this.$scope.selectedCase = selectedCase;
    })
}
angularjs typescript promise ecmascript-5
1个回答
0
投票

如果您需要通过使用then链中的某些东西来更频繁地延迟请求或其他事情,则可以使用以下方法:

/** Can be used within a then-chain to delay the next then in the chain ;) */
export const delayPromise = (ms: number, value: any) => new Promise((resolve) => { setTimeout(() => resolve(value), ms); });
this.caseService.updateCase(updateCaseRequest)
    .then(res => delayPromise(res, 1000)) // Wait 1 second
    .then(response => {
            this.showUpdateComplete();
            this.getCases();
            this.$scope.selectedCase = selectedCase;
        }
    );


© www.soinside.com 2019 - 2024. All rights reserved.