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 }