【发布时间】:2014-09-15 05:10:51
【问题描述】:
我正在使用 bluebird 进行承诺。
我试图承诺download module。
这是我的实现:
Promise = require('bluebird'),
download = require('download');
var methodNameToPromisify = ["download"];
function EventEmitterPromisifier(originalMethod) {
// return a function
return function promisified() {
var args = [].slice.call(arguments);
// Needed so that the original method can be called with the correct receiver
var self = this;
// which returns a promise
return new Promise(function(resolve, reject) {
// We call the originalMethod here because if it throws,
// it will reject the returned promise with the thrown error
var emitter = originalMethod.apply(self, args);
emitter
.on("response", function(data) {
resolve(data);
})
.on("data ", function(data) {
resolve(data);
})
.on("error", function(err) {
reject(err);
})
.on("close", function() {
resolve();
});
});
};
};
download = { download: download };
Promise.promisifyAll(download, {
filter: function(name) {
return methodNameToPromisify.indexOf(name) > -1;
},
promisifier: EventEmitterPromisifier
});
然后使用它:
return download.downloadAsync(fileURL, copyTo, {});
我的问题是它没有下载所有文件(我有一个发送到这个函数的列表),我做错了什么?
【问题讨论】:
-
(a)
download = { download: download };覆盖download = require('download');和(b).on("data ", ...)中是否有该空间是故意的? -
我的错,我会努力解决的。
标签: node.js download promise bluebird