在一次API调用后设置延迟5秒

Pav*_*ati 8 javascript rxjs typescript angular

在我的项目中,我遇到了一个挑战.ie ..我需要逐个调用多个API调用.我在这里使用RxJS flatMap运算符.它正在发挥作用.但我的额外要求是我需要为每个API调用设置10秒的延迟.我使用了'throttle'操作符但是它没有工作.我已经附上了我的代码.任何人都可以告诉我们在我的代码中我做错了什么.

public makeSubmitAPI(oncallData): Observable<any> {
        let orderPayload = this.postAPIService.prepareOrderPayload(oncallData);
        let url = "";
        let orderResponse: any;
        return this.apiCallsService.apiCall('placeOrder', orderPayload, 'post', false)
            .map((orderRes: any) => {
                orderResponse = orderRes;
                url = `orders/${orderResponse.data.id}/progress`;
                let progressPayload = this.postAPIService.prepareProgressAPIData(oncallData, orderResponse.data, this.userType);
                return progressPayload;
            }).pipe(throttle(val=> interval(5000)))
            .flatMap(progressPayload => {
                    return this.apiCallsService.apiCall(url, progressPayload, 'post', false).pipe(throttle(val=> interval(5000)))
            })
            .flatMap(progressResponse => {
                return Observable.combineLatest(
                    orderResponse.data.serviceAddresses.map((address, index) => {
                        let fullfilledAPI = this.postAPIService.prepareFullfilledAPIData(oncallData, orderResponse.data, progressResponse, this.userType, index, orderPayload);
                        return this.apiCallsService.apiCall('fullfillment', fullfilledAPI, 'post', false).map(res => res);
                    })
                )
            });
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

mar*_*tin 7

你可以使用concatMap(而不是flatMap包装API调用)来一个接一个地调用,然后delay()强制你想要的5s延迟:

.concatMap(progressPayload => this.apiCallsService.apiCall(url, progressPayload, 'post', false)
  .pipe(
    delay(5000)
  )
})
Run Code Online (Sandbox Code Playgroud)