Node.js:您可以在流中使用异步函数吗?

Fer*_*gie 22 javascript asynchronous callback stream node.js

考虑以下:

var asyncFunction = function(data, callback) {
  doAsyncyThing(function(data){
    // do some stuff
    return callback(err)
  })
}
fs.createReadStream('eupmc_lite_metadata_2016_04_15.json')
  .pipe(JSONstream.parse())
  .on('data', asyncFunction)   // <- how to let asyncFunction complete before continuing
Run Code Online (Sandbox Code Playgroud)

流是如何知道asyncFunction何时完成的?有没有办法在流中使用异步函数?

Rya*_*inn 18

查看转换流.它们使您能够在块上运行异步代码,然后在完成后调用回调.以下是文档:https://nodejs.org/api/stream.html#stream_transform_transform_chunk_encoding_callback

举个简单的例子,你可以这样做:

const Transform = require('stream').Transform
class WorkerThing extends Transform {
    _transform(chunk, encoding, cb) {
        asyncFunction(chunk, cb)
    }
}

const workerThing = new WorkerThing()

fs.createReadStream('eupmc_lite_metadata_2016_04_15.json')
.pipe(JSONstream.parse())
.pipe(workerThing)
Run Code Online (Sandbox Code Playgroud)


Pas*_*ino 2

我认为这已经足够了:

const Transform = require('node:stream').Transform

const deferTransform = new Transform({
  transform: (chunk, encoding, next) => {
    Promise.resolve(`${chunk.toString().toUpperCase()} `).then((data) =>
      next(null, data)
    );
  },
});


fs.createReadStream('eupmc_lite_metadata_2016_04_15.json')
.pipe(JSONstream.parse())
.pipe(deferTransform)
Run Code Online (Sandbox Code Playgroud)