我对 Nodejs 流还很陌生,所以请耐心等待。
我正在使用节点流pipeline方法来制作流的副本(特别是节点获取的图像,它是可读流)。当我使用同步管道 api时,它工作得很好(例如,我看到管道成功消息控制台日志记录),但我需要使此过程异步,因为我一次处理(制作副本)多个图像。
当我将同步方法包装在 中时util.promisify,它只是永远挂起——承诺似乎永远处于挂起状态(甚至没有抛出一个错误,我可以看到这是最令人沮丧的部分)。我在这里错过了什么吗?接受解决方案的建议,甚至深入了解如何查看问题所在,因为我什至没有收到可以尝试调试的错误消息
这是代码/我尝试过的:
import util from 'util';
import { pipeline, PassThrough } from 'stream';
// synchronous
// confirmed that this works
// the images get copied into the streams array, I get a bunch of 'Pipeline succeeded.' console logs
const streams = allKeys.map(() => pipeline(response.body, new PassThrough(),
(err) => {
if (err) {
console.error('Pipeline failed.', err);
} else {
console.log('Pipeline succeeded.');
}
}
)
);
// asynchronous
// this …Run Code Online (Sandbox Code Playgroud)