Javascript http客户端在响应到达时调用函数

Javascript http客户端在响应到达时调用函数,javascript,angular,rxjs,Javascript,Angular,Rxjs,假设我有一个服务正在向我的API发送post请求,当响应到达时,我希望在流程中执行一个自定义函数 public signin(){ return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).toPromise(); } 公共登录(){ 返回此.http.post(`${env.API_URL}/user/signin

假设我有一个服务正在向我的API发送post请求,当响应到达时,我希望在流程中执行一个自定义函数

public signin(){
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).toPromise();
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password}).toPromise();
}
我尝试使用管道操作符,但由于某些原因,它没有向返回的承诺发送错误

public signin(){
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).pipe(
    tap(
      data => this.customFunction(data),
      error => error
    )
  ).toPromise();
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password})(
水龙头(
data=>this.customFunction(数据),
错误=>错误
)
).toPromise();
}

您可以使用以下代码执行此操作:

public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password })
  .toPromise()
  .then((data) => {
    this.customFunction(data)
  })
  .catch(this.handleError);
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password})
.toPromise()
。然后((数据)=>{
此.customFunction(数据)
})
.接住(这个.把手错误);
}

然后只需定义一个句柄错误函数,或者像使用数据一样使用lambda函数(如果您愿意)

您可以使用以下代码执行此操作:

public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password })
  .toPromise()
  .then((data) => {
    this.customFunction(data)
  })
  .catch(this.handleError);
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password})
.toPromise()
。然后((数据)=>{
此.customFunction(数据)
})
.接住(这个.把手错误);
}

然后只需定义一个句柄错误函数,或者使用lambda函数,就像使用数据一样(如果您愿意)

一个选项是使用如下代码:

getMovies(): Observable<IMovie[]> {
    return this.http.get<IMovie[]>(this.moviesUrl).pipe(
        tap(data => console.log(JSON.stringify(data))),
        catchError(this.handleError)
    );
}
public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).pipe(
    tap(data => this.customFunction(data)),
    catchError(error => console.log(error))
  );
}
getMovies():可观察{
返回this.http.get(this.moviesUrl).pipe(
点击(data=>console.log(JSON.stringify(data)),
catchError(this.handleError)
);
}
然后可以在点击中添加自定义函数

因此,您的代码看起来更像这样:

getMovies(): Observable<IMovie[]> {
    return this.http.get<IMovie[]>(this.moviesUrl).pipe(
        tap(data => console.log(JSON.stringify(data))),
        catchError(this.handleError)
    );
}
public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).pipe(
    tap(data => this.customFunction(data)),
    catchError(error => console.log(error))
  );
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password})(
点击(数据=>this.customFunction(数据)),
catchError(错误=>console.log(错误))
);
}

一个选项是使用如下代码:

getMovies(): Observable<IMovie[]> {
    return this.http.get<IMovie[]>(this.moviesUrl).pipe(
        tap(data => console.log(JSON.stringify(data))),
        catchError(this.handleError)
    );
}
public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).pipe(
    tap(data => this.customFunction(data)),
    catchError(error => console.log(error))
  );
}
getMovies():可观察{
返回this.http.get(this.moviesUrl).pipe(
点击(data=>console.log(JSON.stringify(data)),
catchError(this.handleError)
);
}
然后可以在点击中添加自定义函数

因此,您的代码看起来更像这样:

getMovies(): Observable<IMovie[]> {
    return this.http.get<IMovie[]>(this.moviesUrl).pipe(
        tap(data => console.log(JSON.stringify(data))),
        catchError(this.handleError)
    );
}
public signin() {
  return this.http.post<any>(`${env.API_URL}/user/signin`, { username: user.username, password: user.password }).pipe(
    tap(data => this.customFunction(data)),
    catchError(error => console.log(error))
  );
}
公共登录(){
返回此.http.post(`${env.API_URL}/user/signin`,{username:user.username,password:user.password})(
点击(数据=>this.customFunction(数据)),
catchError(错误=>console.log(错误))
);
}