繁体   English   中英

未处理的 promise 拒绝。 此错误源于在没有 catch 块的情况下抛出异步 function

[英]Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block

我正在处理 javascript 中的大量异步代码。 我可以在单个文件中管理异步代码。 但我不知道从其他 jS 文件中调用 async function 。 我试过但坚持调用 function。 我的代码如下文件 App.js

const analyticsParser = require("./AnalyticsParser");
analyticsParser.getAnalysisData(dbRecord.ObservationValue, analyticalData);

function analyticalData(theData, theError){
  console.log(`_________________________ analyticalData _______________________________`);

  if(theError){
    console.dir(`Error from analyticalData====` + theError)
  }else{
    console.dir(JSON.stringify(theData));
  }
}

AnalyticsParser.js 文件

async function getAnalysisData(analyticData, callback) {
    const documents = analyticData;    
    const poller = await client.beginAnalyzeHealthcareEntities(documents, "en", {
      includeStatistics: true
    });
    const results = await poller.pollUntilDone();
  
    for await (const result of results) {
      console.log(`- Document ${result.id}`);
      if (!result.error) {
        //console.log("\tRecognized Entities: ========" + JSON.stringify(result) + "========");
        callback(result, undefined); 
      } else {
          callback(undefined, result.error)
        }
    }
    
  }

  module.exports =  {
    getAnalysisData
  };

我从其他 JS 文件中调用导出的异步 function。 我收到以下错误,

(node:6264) UnhandledPromiseRejectionWarning: TypeError: inputs.map is not a function
    at convertToTextDocumentInput (E:\\NLP\node_modules\@azure\ai-text-analytics\dist\index.js:4680:19)
    at TextAnalyticsClient.beginAnalyzeHealthcareEntities (E:\\NLP\node_modules\@azure\ai-text-analytics\dist\index.js:4584:26)
    at Object.getAnalysisData (E:\\NLP\HealthDataAnalytics.js:10:33)
    at patientDocuments (E:\\NLP\App.js:26:23)
    at E:\\NLP\DbConnect.js:44:7
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6264) 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:6264) [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

如何处理异步 function 调用?

要使用 async/await 方法处理代码中出现的错误,请使用 try/catch 块来包装通常会返回 Promise 的代码。 请参见下面的示例。

function async callRemoteApi(){
  try {
    const response = await client.callApi()
  } catch(error) {
    console.log(error)
  }
  return response;
}

这是一个更深入的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM