【发布时间】:2019-11-29 12:00:10
【问题描述】:
我有一个 API 可以返回“RequestedTooSoon”响应并包含“NotBefore”时间戳。
我的目标是引入人为的延迟,以便让任何消费者都觉得请求花费的时间比实际时间长。响应应该延迟到“NotBefore”时间过去。
// Consumer:
verifyAuth() {
authService.verify(...).subscribe(...)
}
在我的AuthService.verify(...) 方法中,我试图限制响应,但是仔细阅读RxJS 文档,很明显throttle 不是我正在寻找的运算符:
this.apiHttp.post<VerificationResponse>('api/verify', ...)
.pipe(
throttle(response => {
const nowInMs = Date.now();
const throttle = response.NotBefore.getTime() - nowInMs;
return timer(throttle);
})
)
我无法确定合适的运算符(如果存在的话)。
是否有开箱即用的运算符可以实现我所追求的?还是有更合适的方法?
我的下一步将是实现一个接受durationSelector 的delay 运算符。
【问题讨论】: