【问题标题】:Correct way of using Async/await in node.js在 node.js 中使用 Async/await 的正确方法
【发布时间】:2019-04-21 01:10:27
【问题描述】:

我已经编写了一些代码来调用一个过程,它的 OUT 参数类型是游标(ResultSet),所以我必须从 ResultSet 中获取数据,为此我编写了一个从 ResultSet 中提取数据的函数(fetchRowsFromRS()) .

  1. 我在 fetchRowsFromRS() 中使用了 return 语句,但没有返回 任何东西,变得不确定。
  2. 当我调用 fetchRowsFromRS() 时,控制不会暂停执行 下一行代码(我使用过 Async/await)这是必需的,因为我想在下一行中使用提取的数据。

我的代码有什么错误?

db.js

    connection.execute(plsql,bindvars,options,async function (err, result) {
          if (err) {
            console.log(err);
            console.error(err.message);
            doRelease(connection);
            return;
          }

      if(result.outBinds.OUT_STATUS=='SUCCESS'){

        if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

        loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

        loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
       }else{

   loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

   loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User 
       logged in successfully');
       var numRows=20;
       //calling function to fetch data from ResultSet
       var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
    console.log('----------'+rsData);//giving undefined

    //here i want to use ResultSet Data

        }
      }
  })

从 ResultSet 中提取数据的函数(不返回任何内容)

function fetchRowsFromRS(connection, resultSet, numRows) {
     resultSet.getRows(numRows,function (err, rows) {
          if (err) {
            console.error(err);
            doClose(connection, resultSet);   // always close the ResultSet
          } else if (rows.length > 0) {
          console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
          console.log(rows); //getting data here


          if (rows.length === numRows)      // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows);
          else
            doClose(connection, resultSet); // always close the ResultSet


        } else { // no rows
          doClose(connection, resultSet);   // always close the ResultSet
        }
        return rows;
      });
  }

【问题讨论】:

  • “我在fetchRowsFromRS中使用了return语句”:不,你没有。您在回调函数中有一个return,这是一个不同的函数。
  • 什么是connection.execute?鉴于它需要一个节点样式的回调,它似乎没有承诺感知。不要将async function 作为回调传递。
  • 有这么多的例子和教程,你为什么不花时间学习javascript的基础知识。您没有使用 async-await,只是要求其他人解决您的问题。
  • @NAVIN 对不起,我正在学习理解javascript和node.js

标签: javascript node.js async-await


【解决方案1】:
function fetchRowsFromRS(connection, resultSet, numRows) {
  return resultSet
    .getRows(numRows)
    .then(function(rows) {
      if (rows.length > 0) {
        console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
        console.log(rows) //getting data here
        if (rows.length === numRows)
          // might be more rows
          fetchRowsFromRS(connection, resultSet, numRows)
        else doClose(connection, resultSet) // always close the ResultSet
      } else {
        // no rows
        doClose(connection, resultSet) // always close the ResultSet
      }
      return rows
    })
    .catch(function(err) {
      if (err) {
        console.error(err)
        doClose(connection, resultSet) // always close the ResultSet
      }
    })
}

如果 resultSet.getRows(numRows) 返回一个承诺而不是接受回调,或者 resultSet.getRows(numRows) 有一些替代方案,则上述方法将起作用返回一个承诺。

等待 fetchRowsFromRS(connection, resultSet, numRows)

只有在 fetchRowsFromRS(connection, resultSet, numRows) 返回一个 Promise 时才有效,这反过来又要求函数内定义的所有内容都返回一个 Promise。

使用新的承诺

function fetchRowsFromRS(connection, resultSet, numRows) {
  return new Promise(function(resolve, reject) {
    resultSet.getRows(numRows, function(err, rows) {
      if (err) {
        console.error(err)
        doClose(connection, resultSet) // always close the ResultSet
        reject(err)
      } else if (rows.length > 0) {
        console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
        console.log(rows) //getting data here

        if (rows.length === numRows)
          // might be more rows
          fetchRowsFromRS(connection, resultSet, numRows)
        else doClose(connection, resultSet) // always close the ResultSet
      } else {
        // no rows
        doClose(connection, resultSet) // always close the ResultSet
      }
      resolve(rows)
    })
  })
}

【讨论】:

  • 避免使用Promise constructor antipattern!在你的第一个 sn-p 中,确保 return 来自 then 回调的内部承诺,否则它们将不会被等待。
  • @Jeevan 非常感谢!给你的建议。我用过 resultSet.getRows(numRows) 它返回一个承诺。
【解决方案2】:

fetchRowsFromRS 没有返回任何内容。您似乎没有正确使用 async/await 。 resultSet.getRows 使用回调。将其更改为返回一个 Promise 或 wrap 它在一个 Promise 中并从 fetchRowsFromRS 返回。然后你就可以await它了。

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 2018-12-16
    • 2018-04-13
    • 1970-01-01
    • 2018-06-10
    • 2014-03-16
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多