我尝试使用instance.my_file.read(instance.my_file.DEFAULT_CHUNK_SIZE)
promise库将几个Q
方法链接在一起,以更改我已经获得的对象数组。我想我已经接近了,但是我在某个地方犯了一个错误而且收到了错误:
async
阵列是这样的:
resolve(sizes);
^
TypeError: object is not a function
然后我有两个函数,我试图按顺序调用。就像一个人完成时var sizes = [{
"oldName": "10",
"newName": "10_1",
"sizeExistsInDest": false,
"sizeExistsInSrc": false,
"filesInSrc": [],
"dirsInDest": [],
"filesInDest": [],
"filesToCopy": []
}.....]; //ten or so elements
,然后拨打下一个。
then
当我打电话
var getSrcDirs= function(resolve) {
async.eachLimit(sizes, 1, function(size, callback){
var srcDir = path.join(src, size.oldName);
if (exists(srcDir)){ //exists is from the is-there module
size.sizeExistsInSrc = true;
}
callback();
}, function(){
resolve(sizes);
});
};
var getSrcFiles = function(resolve){
async.each(sizes, function(size, callback){
if(size.sizeExistsInSrc){ //only process if the "exists" flag has been set to true
var fullSrc = path.join(src, size.oldName); //format like 'rootSrc/10'
fs.readdir(fullSrc, function(err, srcFiles){
size.filesInSrc = srcFiles;
callback(); //re-enter each loop once files have been set to `filesInSrc` array
});
}else{
callback(); //if directory doesn't exist, re-enter each loop
}
}, function(){
resolve(sizes);
});
};
var getFileData = function () {
var deferred = Q.defer();
getSrcDirs(deferred.resolve);
return deferred.promise;
};
var srcFiles = getFileData()
的控制台打印正确显示已修改的srcFiles
数组。但是,在添加sizes
。
then
我想我应该看到文件已添加到var srcFiles = getFileData().then(getSrcFiles);
数组中,我得到了上面指定的错误。我想连接几个sizes
s,但我似乎无法多次修改我的数组。