在异步中使用返回值在另一个函数中等待

时间:2019-04-05 13:14:41

标签: javascript node.js excel asynchronous async-await

因此,我试图在另一个函数中访问thermostatRow和在其中定义的const,并且尝试通过在该函数中记录const来测试其是否正常工作,因此我知道可以将其分解为不同的部分,根据需要将这些部分推入阵列。

我尝试了3件事,其中2件事在下面的代码示例中,第3则返回了Promise { <pending> },但我找不到该示例。

 const worksheet = workbook.getWorksheet("sheet1"); 
// worksheet is defined in another function earlier in the file that is called  asynchronously before this one. 
    async function dataFormat(worksheet) {
      csvWorkbook = workbook.csv.readFile("./uploads/uploadedFile.csv");
      await csvWorkbook.then(async function() {
        let restarts = 0;
        let nullCounts = true;
        let thermostatRows = [];
        // don't create arrays of current temp here ! do it in a diff function
        // const [serial,date,startDate,endDate,thing3,thing4,thing5] = firstTemps

    worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
      if (rowNumber > 6) {
        thermostatRows.push((row.values));
      }
    });
    thermostatRows.map(thermostatRow => {
      [,date,time,SystemSetting,systemMode,calendarEvent,ProgramMode,coolSetTemp,HeatSetTemp,currentTemp,currentHumidity,outdoorTemp,windSpeed,coolStage1,HeatStage1,Fan,DMOffset,thermostatTemperature,thermostatHumidity,thermostatMotion] = thermostatRow
      return thermostatRow// Push current temps , heatset and cool set into seperate arrays and use those arrays to run the overheat() function
    })
  })
};

const dataResult = async (worksheet) => {
  let result = await dataFormat(worksheet);
  console.log(result);
}

这什么都不记录。

所以我也尝试过:

const dataResult = async () => {
  let result = await dataFormat();
  console.log(result);
}

dataResult()

并收到此错误:

(node:11059) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'eachRow' of undefined
    at /Users/jasonharder/coding/projects/hvacdoctor/controllers/hvacdoctor.js:69:15
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

我想念发生了什么事?

注意:我对代码进行了一些重构-现在返回的是undefined。 注意:附加在定义工作表的位置(在文件中定义)

0 个答案:

没有答案