即使typescript中发生错误,循环仍保持运行

即使typescript中发生错误,循环仍保持运行,typescript,nestjs,Typescript,Nestjs,下面是typescript代码 我有一个for循环,它的值通过url传递给API。httpservice是nestjs httpservice for (const trackingCode of fileNames) { url = url + "?trackingnumber=" + trackingCode; var response = await this.httpService.get(url, { headers: headersReques

下面是typescript代码

我有一个for循环,它的值通过url传递给API。httpservice是nestjs httpservice

for (const trackingCode of fileNames) {
     url = url + "?trackingnumber=" + trackingCode;
     var response = await this.httpService.get(url, { headers: headersRequest }).toPromise();

}
让我们想象文件名在数组下面

fileNames = ["ABC001","ABC002"];
ABC001代码未在其侧定义,ABC002已定义

启动循环时,首先将ABC001传递给API。它抛出下面的错误,因为他们没有代码

(node:27932) UnhandledPromiseRejectionWarning: Error: Request failed with status code 400
    at createError (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/adapters/http.js:237:11)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:27932) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:27932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
但是由于这个错误,循环没有运行

然后,我将api调用部分移动到具有错误处理的独立函数,并从主函数调用该函数

 async myFunc(url: string, headersRequest: any) {

    try {
      var response = await this.httpService.get(url, { headers: headersRequest }).toPromise();
      return response;
    } catch (ex) { 
      return null;
    } finally {
    }

  }
  var response = await this.myFunc(url, headersRequest);

  if (response != null) {
       //my logic goes here
  }
即使使用try-catch块,循环也不会进一步运行。简单地说,当因为ABC001而抛出错误时,它将不会为ABC002运行

如何处理这类问题??我希望保持循环运行,即使发生任何错误

编辑

这就是我在主函数中使用“myFunc”的方式

 async myFunc(url: string, headersRequest: any) {

    try {
      var response = await this.httpService.get(url, { headers: headersRequest }).toPromise();
      return response;
    } catch (ex) { 
      return null;
    } finally {
    }

  }
  var response = await this.myFunc(url, headersRequest);

  if (response != null) {
       //my logic goes here
  }

我能想到两件事,首先这一行代码对我来说很可疑:

url = url + "&trackingnumber=" + trackingCode;
因为它会不断地在循环中添加更多trackingnumber,而不是替换它。所以我预计不止一次失败


另一方面,如果这只是示例代码,而真正的代码运行正确,那么可能是
this.httpService.get
没有正确处理来自axios库的承诺。try-catch-around异步代码无法捕获在其他库中抛出且未正确处理的错误,例如,它们未正确等待值或.catch未在promise上使用。

使用
myFunc
@Anatoly显示最终代码我添加了您要求的代码。你能发现这有什么问题吗?老实说,我对typescript还不熟悉,这就是我遇到的问题。根据你的回复,url=url+blablablabla是个问题。我犯的愚蠢的错误。非常感谢你帮助我。。。。