承诺/捕获没有捕获promise.reject

时间:2017-12-12 00:31:19

标签: node.js es6-promise tedious

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

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.' });
  });

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

1 个答案:

答案 0 :(得分:0)

我不完全确定您使用代码的request.on('returnValue', ...)部分正在做什么,但如果您只想返回一个已解决价值的承诺是rows数据,那么您需要实际创建一个promise并返回它然后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);
            }
        });
    });
}