angular http post 重试

 1   // import { retry, retryWhen, catchError, map, tap, delayWhen, scan, takeWhile } from 'rxjs/operators';
 2   // import { timer } from 'rxjs/observable/timer';
 3   //带重试的http post请求例子
 4   test() {
 5     let httpURL = "http://localhost:3000/test"
 6     this.http.post(httpURL, {}).timeout(1800000).pipe(map((val, index) => {
 7       console.log("index", index)
 8       if (val["success"] != true) {
 9         throw val
10       }
11       return val
12     }), retryWhen(errors => {
13       //  console.log("retryWhen errors", errors)
14       return errors.pipe(
15 
16         // tap(val => console.log("retryWhen", val)),
17         delayWhen(() => timer(3 * 1000)),//延迟多少秒后重试
18         scan(count => {
19           //  console.log("count", count)
20           return count + 1
21         }, 0)
22         ,
23         takeWhile(count => {
24           return count < 3 //重试次数
25         })
26       )
27     }
28     )).subscribe(res => {
29 
30       console.log(res)
31     }, err => {
32 
33       console.log(err)
34     })
35   }

 

posted @ 2020-07-28 17:44  风轻2022  阅读(395)  评论(0编辑  收藏  举报