【问题标题】:Catch errors in promise resolved with stream concatenation in node js通过 node js 中的流连接解决了 Promise 中的错误
【发布时间】:2018-12-20 18:40:38
【问题描述】:

我需要在 Promise 解析中捕获流事件中引发的错误。类似的东西

function foo(){
 return new Promise((resolve,reject) => {
  resolve(res.pipe(transfomrStream).pipe(through2.obj(function(obj, enc, callback) {
    on('end', ()=>{ 
        await httpReq(...)
          .then((crudRes) => assert.strictEqual(somerthing))
      })
     )
   })
 })
}

我如何捕捉断言失败?我试图在最后一个 pipe() 或函数调用中返回错误,但我只得到 unhandled promise reject

【问题讨论】:

    标签: node.js error-handling stream throw through2


    【解决方案1】:

    如果你用 try catch 包围你的代码,你至少可以看到你的错误是什么:

    function foo(){
    try{
     return new Promise((resolve,reject) => {
      resolve(res.pipe(transfomrStream).pipe(through2.obj(function(obj, enc, callback) {
        on('end', ()=>{ 
            await httpReq(...)
              .then((crudRes) => assert.strictEqual(somerthing))
          })
         )
       })
     })
     }
     catch(e) {
      console.log(e)
      reject()
    }
    }
    

    另外,如果您有await,则不能使用.then()。如果你正在等待,你将不得不让你的函数async

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多