是否可以在 Angular 1.7 中创建新的 Promise 对象?
Is it possible to create new Promise object in Angular 1.7?
我有一个当前使用 Angular 1.7 的应用程序,我们有一个 IHTTPPromise
(updateCase),然后我们在 then()
方法解析后处理代码
在第一个 then()
之前,我想链接另一个 then()
以设置 x 毫秒的等待时间
我不能就此使用 setTimeout()
,因为那不是 return 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;
})
}
如果您需要通过使用 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;
}
);
编辑:不能在 angularjs 上下文中使用 - 请参阅评论
使用 $timeout 服务:
this.caseService.updateCase(updateCaseRequest)
.then( response => {
return this.$timeout( (_ => response) , delay );
}).then( response => {
this.showUpdateComplete();
this.getCases();
this.$scope.selectedCase = selectedCase;
})
调用$timeout
的return值是一个promise,延迟时间过去后会被resolve,如果提供超时函数,则执行
有关详细信息,请参阅
我有一个当前使用 Angular 1.7 的应用程序,我们有一个 IHTTPPromise
(updateCase),然后我们在 then()
方法解析后处理代码
在第一个 then()
之前,我想链接另一个 then()
以设置 x 毫秒的等待时间
我不能就此使用 setTimeout()
,因为那不是 return 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;
})
}
如果您需要通过使用 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;
}
);
编辑:不能在 angularjs 上下文中使用 - 请参阅评论
使用 $timeout 服务:
this.caseService.updateCase(updateCaseRequest)
.then( response => {
return this.$timeout( (_ => response) , delay );
}).then( response => {
this.showUpdateComplete();
this.getCases();
this.$scope.selectedCase = selectedCase;
})
调用$timeout
的return值是一个promise,延迟时间过去后会被resolve,如果提供超时函数,则执行
有关详细信息,请参阅