在 ionic 6 中为 http 请求设置超时

Setting a timeout for a http request in ionic 6

提问人:user6781 提问时间:11/18/2023 更新时间:11/18/2023 访问量:10

问:

我需要为我的 http 请求设置超时。我在网上找到的内容如下:

 public async GETRequest(url: string, parameters: any, headers: any): Promise<any> {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        this.http.get(url, parameters, headers)
          .then(
          (res:any) => {
              console.error('res is: ', res);
              resolve(res);
          },
          (error:any) => {
              console.error('error is: ', error);
              reject(error);
          }
          );}
          , 1000);
     });
  }

但是,它没有按预期工作!有谁知道出了什么问题?谢谢!

ionic-framework 超时 httprequest

评论

0赞 Leroy 11/19/2023
你想完成什么?您是试图延迟 http 请求,还是尝试在 http 请求上设置超时(例如,服务器必须在 n 秒内响应)。你在 ionic 中使用什么:Angular、React 或 Vue?我猜是有角度的。Angular 在运行时返回一个 Observable,对吗?不是承诺。this.http.get()

答: 暂无答案