如何处理 RxJs 超时完成 - Angular HttpClient
How to handle RxJs timeout complete - Angular HttpClient
如何通过超时运算符检测错误?我想在服务器没有响应时显示警报或类似的东西。
我的拦截器中有一段类似的代码:
this.http.post('http://localhost:3000/api/core', data)
.pipe(
timeout(30000),
map((response: any) => { // Success...
return response;
}),
catchError((error) => { // Error...
// Timeout over also handled here
// I want to return an error for timeout
return throwError(error || 'Timeout Exception');
}),
finalize(() => {
console.log('Request it is over');
})
);
["rxjs": "^6.0.0", "@angular/http": "^6.0.3",]
您可以使用 timeoutWith 运算符来实现此目的。
return this.http.post('http://localhost:3000/api/core', data).pipe(
timeoutWith(3000, observableThrowError(new Error('Http Timeout exceeds'))),
map((response: any) => { // Success...
return response;
}),
catchError((error: HttpErrorResponse) => this.handleError(error))
);
这个有效
import { throwError, TimeoutError } from 'rxjs';
catchError((error) => { // Error...
// Handle 'timeout over' error
if (error instanceof TimeoutError) {
return throwError('Timeout Exception');
}
// Return other errors
return throwError(error);
})
我在我的拦截器上实现了它,它具有其他功能,代码 here
如何通过超时运算符检测错误?我想在服务器没有响应时显示警报或类似的东西。
我的拦截器中有一段类似的代码:
this.http.post('http://localhost:3000/api/core', data)
.pipe(
timeout(30000),
map((response: any) => { // Success...
return response;
}),
catchError((error) => { // Error...
// Timeout over also handled here
// I want to return an error for timeout
return throwError(error || 'Timeout Exception');
}),
finalize(() => {
console.log('Request it is over');
})
);
["rxjs": "^6.0.0", "@angular/http": "^6.0.3",]
您可以使用 timeoutWith 运算符来实现此目的。
return this.http.post('http://localhost:3000/api/core', data).pipe(
timeoutWith(3000, observableThrowError(new Error('Http Timeout exceeds'))),
map((response: any) => { // Success...
return response;
}),
catchError((error: HttpErrorResponse) => this.handleError(error))
);
这个有效
import { throwError, TimeoutError } from 'rxjs';
catchError((error) => { // Error...
// Handle 'timeout over' error
if (error instanceof TimeoutError) {
return throwError('Timeout Exception');
}
// Return other errors
return throwError(error);
})
我在我的拦截器上实现了它,它具有其他功能,代码 here