gpt4 book ai didi

Node.js - Gunzip 已读取的文件异步问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:02 24 4
gpt4 key购买 nike

对于 node.js 和异步处理方式来说相对较新,到目前为止,我已经能够使用 Promise 来使用 fs readFile 来读取文件,但我还没有让 zlib Gunzip 工作。用 Coffeescript 编写:

   promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (file) ->
promisifyRun(zlib, 'Gunzip', file)
.then (data) ->
console.log "HELLO"
return data
.catch respondError res

promisfyRun 用于 promise 单个函数(我没有编写它,但它确实有效)。我已经成功地将它用于 fs.readFile 组件,如下所示:

   promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (data) ->
return data
.catch respondError res

这工作得很好,它等待文件被打开然后继续。 “data”包含文件的主体。我认为合并gunzip 组件应该是一个非常合乎逻辑的扩展,但到目前为止这一直是一个挑战。

我查看了一些 npmgunzip 模块。看起来最有趣的是 gunzip-maybezlib.Gunzip (我在这里尝试)。

此特定情况的错误消息是:

"Unhandled rejection Error: Can't set headers after they are sent."

我认为这与进程已经完成有关,因为它是异步的

更新——完整的堆栈跟踪:

Unhandled rejection Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11) at ServerResponse.header (/Users/jcook/project/node_modules/express/lib/response.js:725:10) at ServerResponse.send (/Users/jcook/project/node_modules/express/lib/response.js:170:12) at ServerResponse.json (/Users/jcook/project/node_modules/express/lib/response.js:256:15) at ServerResponse.send (/Users/jcook/project/node_modules/express/lib/response.js:158:21) at /Users/jcook/project/.tmp/lib/util.js:40:22 at tryCatcher (/Users/jcook/project/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Users/jcook/project/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/Users/jcook/project/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/Users/jcook/project/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/Users/jcook/project/node_modules/bluebird/js/release/promise.js:689:18) at Async._drainQueue (/Users/jcook/project/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/Users/jcook/project/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/Users/jcook/project/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:672:20) at tryOnImmediate (timers.js:645:5) at processImmediate [as _immediateCallback] (timers.js:617:5)

最佳答案

当您看到发送后无法设置 header 时,几乎总是因为端点响应后您的请求处理程序仍在执行,而 IME 99% 与代码无关等待回调完成。

首先,如果不链接你的 Promise,你就会否定使用 Promise 的很多好处。

promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (file) ->
// Just return another promise here.
return promisifyRun(zlib, 'Gunzip', file)
.then (data) ->
console.log "HELLO"
return data
.catch respondError res

其次,您正在从回调内部运行返回数据。我的猜测是您正在尝试在另一端使用返回值,如下所示:

run = ()->
promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (file) ->
// Just return another promise here.
return promisifyRun(zlib, 'Gunzip', file)
.then (data) ->
console.log "HELLO"
return data
.catch respondError res

myData = run()

这不起作用,因为从 Promise 返回的内容是异步的。您需要处理异步响应:

run = ()->
promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (file) ->
// Just return another promise here.
return promisifyRun(zlib, 'Gunzip', file)
// We remove the rest of the function,
// so it just returns the promise above.

run().then (data)->
console.log data

一旦 CS 2.0 发布,您将能够使用 ES6 async/await:

data = await run()

关于Node.js - Gunzip 已读取的文件异步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com