gpt4 book ai didi

angular - flatMap 中请求之间的延迟

转载 作者:太空狗 更新时间:2023-10-29 18:08:17 25 4
gpt4 key购买 nike

我有几个请求必须一一调用:

this.firstRequest()
.flatMap(resp => {
//check response
if(!response.success) {
return Observable.throw('some message');
}

return this.secondRequest();
})
.subscribe(...)

firstRequest() {
// build params and call http service
return this.service.getData(...)
.do(r => {
if(r.success) {
this.localStorage.save('code', r.code)
}
})
.delay(5000);
}

如您所见,第一个请求和第二个请求之间存在延迟。但是,只有当 success 字段为真时,我才需要延迟。我该怎么做?

最佳答案

下面是一些应该有帮助的代码:

  ngAfterViewInit() {
this.request1()
.flatMap((response:any)=>{
console.log(response);
if(!response.success){
return this.request2().delay(10000);
}
return this.request2();
})
.subscribe((response:any)=>{
console.log(response.value);
});
}

request1():Observable<any>{
return Observable.create(observer=>{
for(let i=0; i<2; i++){
let success =i%2==0? true:false;
observer.next({value:"request1:"+i,success:success});
}
});
}

request2():Observable<any>{
return Observable.create(observer=>{
for(let i=0; i<2; i++){
observer.next({value:"request2:"+i});
}
});
}
}

结果是: enter image description here

关于angular - flatMap 中请求之间的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44839823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com