-1

我的异步函数返回Promise.reject,但调用者无法捕获它。
我正在使用繁琐和快速。
我错了什么?

const connection = require('tedious').Connection;
const Request = require('tedious').Request;
const TYPES = require('tedious').TYPES;

async function doInsertDansok() {

  connection.on('connect', (err)=> {
      if (err) {
        log.error('connection error:', err);
        return new Promise.reject(err);
      } else {
        log.info('connection success');
        let request = new Request('SP_TEST_OUTPUT_PARAM 1, @o_retKey OUT, @o_errMsg OUT, @o_retReceiptInfo OUT', 
          (err, rowCount, rows)=> {
          if (err) {
            log.error('request error:', err);  //<== this log is written.
            return new Promise.reject(err);
          } else {
            log.info(rowCount + " rows(s) returned");
          }
        });
        request.addOutputParameter('o_retKey', TYPES.Int);
        request.addOutputParameter('o_errMsg', TYPES.VarChar, {length:100});
        request.addOutputParameter('o_retReceiptInfo', TYPES.VarChar, {length:255});
        request.on('returnValue', (parameterName, value, metadata) => {
          log.info(parameterName + ' = ' + value);
          return new Promise.resolve(parameterName, value);
        });
        connection.callProcedure(request);
      }
  })
}

exports.insertDansok = function (req, res) {
  doInsertDansok()
  .then((paramter, value)=> {
    log.info('doInsertDansok success');
    res.status(200).json({ message: 'insertdansok success.' });
  })
  .catch((e)=> {
    log.error('insertDansok Failed', e);  //<== I thought this log be written.  But not.
    res.status(400).json({ message: 'insertdansok failed.' });
  });

我做了错误的 SQL 语句Request
我的期望是.catch((e)=>catchdoInsertDansok函数的拒绝。但没有。

4

1 回答 1

0

我不完全确定你在用request.on('returnValue', ...)你的代码部分做什么,但如果你只是想返回一个已解决的值是rows数据的承诺,那么你需要实际创建一个承诺并返回它,然后resolve()reject()它基于关于异步操作的结果,如下所示:

const connection = require('tedious').Connection;
const Request = require('tedious').Request;
const TYPES = require('tedious').TYPES;

function doInsertDansok() {

    return new Promise((resolve, reject) => {
        connection.on('connect', (err) => {
            if (err) {
                log.error('connection error:', err);
                reject(err);
            } else {
                log.info('connection success');
                let request = new Request('SP_TEST_OUTPUT_PARAM 1, @o_retKey OUT, @o_errMsg OUT, @o_retReceiptInfo OUT',
                    (err, rowCount, rows) => {
                        if (err) {
                            log.error('request error:', err); //<== this log is written.
                            reject(err);
                        } else {
                            log.info(rowCount + " rows(s) returned");
                            resolve(rows);
                        }
                });
                request.addOutputParameter('o_retKey', TYPES.Int);
                request.addOutputParameter('o_errMsg', TYPES.VarChar, {length: 100});
                request.addOutputParameter('o_retReceiptInfo', TYPES.VarChar, {length: 255});
                request.on('returnValue', (parameterName, value, metadata) => {
                    log.info(parameterName + ' = ' + value);
                });
                connection.callProcedure(request);
            }
        });
    });
}
于 2017-12-12T00:48:40.703 回答