NodeJS 如何从多级嵌套回调中获取值?[duplicate]

niwlg2el  于 2023-02-03  发布在  Node.js
关注(0)|答案(1)|浏览(127)
    • 此问题在此处已有答案**:

(41个答案)
1小时前关闭。
我正在使用mssql npm库,它正在工作,但是我很难从子级回调中获得返回的记录集。
当记录集是多层回调时,如何取回记录集?

let storedRecordset = await doQuery();

async function doQuery(){

     let recordset;

     const ps = new sql.PreparedStatement(/* [pool] */)
     ps.input('param', sql.Int)
     ps.prepare('select @param as value', err => {
         // ... error checks

         ps.execute({param: 12345}, (err, result) => {
             // ... error checks
 
             recordset = result;        
     
             // release the connection after queries are executed
             ps.unprepare(err => {
                 // ... error checks

                 return recordset;

             })
         })
     })

}

谢谢

gpnt7bae

gpnt7bae1#

let storedRecordset = await doQuery();

function doQuery() {
  return new Promise((r) => {
    let recordset;

    const ps = new sql.PreparedStatement(/* [pool] */);
    ps.input("param", sql.Int);
    ps.prepare("select @param as value", (err) => {
      // ... error checks

      ps.execute({ param: 12345 }, (err, result) => {
        // ... error checks

        recordset = result;

        // release the connection after queries are executed
        ps.unprepare((err) => {
          // ... error checks

          r(recordset);
        });
      });
    });
  });
}

相关问题