繁体   English   中英

在 node.js 中对流使用 promise

[英]Using promises with streams in node.js

我重构了一个简单的实用程序来使用 promise。 它从网上获取 pdf 并将其保存到磁盘。 一旦保存到磁盘,它应该然后在 pdf 查看器中打开文件。 该文件出现在磁盘上并且有效,shell 命令打开 OSX 预览应用程序,但会弹出一个对话框,抱怨该文件为空。

将文件流写入磁盘后,执行 shell 函数的最佳方法是什么?

// download a pdf and save to disk
// open pdf in osx preview for example
download_pdf()
  .then(function(path) {
    shell.exec('open ' + path).code !== 0);
  });

function download_pdf() {
  const path = '/local/some.pdf';
  const url = 'http://somewebsite/some.pdf';
  const stream = request(url);
  const write = stream.pipe(fs.createWriteStream(path))
  return streamToPromise(stream);
}

function streamToPromise(stream) {
  return new Promise(function(resolve, reject) {
    // resolve with location of saved file
    stream.on("end", resolve(stream.dests[0].path));
    stream.on("error", reject);
  })
}

在这一行

stream.on("end", resolve(stream.dests[0].path));

您正在立即执行resolve ,并且调用resolve结果(这将是未定义的,因为这是resolve返回的结果)被用作stream.on的参数 - 根本不是您想要的,对吧。

.on的第二个参数需要是一个函数,而不是调用函数的结果

因此,代码需要

stream.on("end", () => resolve(stream.dests[0].path));

或者,如果你是老派:

stream.on("end", function () { resolve(stream.dests[0].path); });

另一种老派的方式就像

stream.on("end", resolve.bind(null, stream.dests[0].path));

不,不要那样做 :p 看评论

经过多次尝试,我找到了一个一直运行良好的解决方案。 有关更多信息,请参阅 JSDoc 评论。

/**
 * Streams input to output and resolves only after stream has successfully ended.
 * Closes the output stream in success and error cases.
 * @param input {stream.Readable} Read from
 * @param output {stream.Writable} Write to
 * @return Promise Resolves only after the output stream is "end"ed or "finish"ed.
 */
function promisifiedPipe(input, output) {
    let ended = false;
    function end() {
        if (!ended) {
            ended = true;
            output.close && output.close();
            input.close && input.close();
            return true;
        }
    }

    return new Promise((resolve, reject) => {
        input.pipe(output);
        input.on('error', errorEnding);

        function niceEnding() {
            if (end()) resolve();
        }

        function errorEnding(error) {
            if (end()) reject(error);
        }

        output.on('finish', niceEnding);
        output.on('end', niceEnding);
        output.on('error', errorEnding);
    });
};

用法示例:

function downloadFile(req, res, next) {
  promisifiedPipe(fs.createReadStream(req.params.file), res).catch(next);
}

更新。 我已将上述功能发布为 Node 模块: http : //npm.im/promisified-pipe

在最新的 nodejs 中,特别是流 v3,你可以这样做:

const finished = util.promisify(stream.finished);

const rs = fs.createReadStream('archive.tar');

async function run() {
  await finished(rs);
  console.log('Stream is done reading.');
}

run().catch(console.error);
rs.resume(); // Drain the stream.

https://nodejs.org/api/stream.html#stream_event_finish

另一种解决方案可能如下所示:

const streamAsPromise = (readable) => {
  const result = []
  const w = new Writable({
    write(chunk, encoding, callback) {·
      result.push(chunk)
      callback()
    }
  })
  readable.pipe(w)
  return new Promise((resolve, reject) => {
    w.on('finish', resolve)
    w.on('error', reject)
  }).then(() => result.join(''))
}

你可以像这样使用它:

streamAsPromise(fs.createReadStream('secrets')).then(() => console.log(res))

这可以使用 promisified 管道函数很好地完成。 管道还提供额外的功能,例如清理流。

const pipeline = require('util').promisify(require( "stream" ).pipeline)

pipeline(
  request('http://somewebsite/some.pdf'),
  fs.createWriteStream('/local/some.pdf')
).then(()=>
  shell.exec('open /local/some.pdf').code !== 0)
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM