简体   繁体   中英

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

I'm dealing with lot of async code in javascript. I can manage async code in single file. But I don't have idea to called async function from other jS file. I tried but stuck in calling the function. My Code as below File 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 file

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
  };

I calling exported async function from other JS file. I got following error,

(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

How do I deal with async function call?

To handle errors that come in code using the async/await method, you use a try/catch block to wrap the code that would normally return a Promise. See the example below.

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

Here is a more in-depth example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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