相对较新的node.js和异步处理方式,到目前为止我已经能够使用promise读取使用fs readFile的文件,但我没有'没有任何运气让zlib Gunzip工作。写作的CoffeeScript:Node.js - Gunzip已读文件异步问题
promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (file) ->
promisifyRun(zlib, 'Gunzip', file)
.then (data) ->
console.log "HELLO"
return data
.catch respondError res
promisfyRun是有前途的一个单一的功能(我没有写出来,但它的工作)。我已经设法成功地使用它的fs.readFile组件,就像这样:
promisifyRun(fs, 'readFile', filepath, 'utf-8')
.then (data) ->
return data
.catch respondError res
这只是正常,等待要打开的文件,然后继续。 'data'包含文件的主体。我认为它应该是一个非常合理的扩展,以纳入gunzip组件,但到目前为止这一直是一个挑战。
我看了几个npm gunzip模块。似乎最有趣的那个是gunzip-maybe或zlib.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)
什么是完整的堆栈跟踪? – LEQADA
请发送发送标题的代码。你的'util。tryCatcher'(或使用它的代码)可能是问题。 – Bergi