node.js - 使用Node JS中的流串联解决Promise中的错误

标签 node.js error-handling stream throw through2

我需要捕获在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()或函数调用中返回错误,但是我只得到未处理的 promise 拒绝

最佳答案

如果用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

关于node.js - 使用Node JS中的流串联解决Promise中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866118/

相关文章:

javascript - 如何在node.js + ajax中向用户发送自定义错误消息

Node.js 在 CSV 模块中管道 gzip 流

stream - Streaming 在 Streaming SIMD Extensions (SSE) 中代表什么?

node.js - Node + mongo-native : how to find and update value in collection?

javascript - 为什么这个抓取返回未定义?

node.js - 未处理的PromiseRejection警告: Error: WHERE parameter "username" has invalid "undefined" value in postman

java - 如何将以下代码转换为流

javascript - 使用 CoffeeScript 时使用 util.inherits() "breaks"原型(prototype)制作

ios - Swift 2.2 错误处理和音频

javascript - 这是什么错误?