I'm trying to use the Q
promise library to chain together a couple of async
methods to change an array of objects I've got. I think I'm close, but I'm making a mistake somewhere and getting the error:
resolve(sizes); ^ TypeError: object is not a function
The array is like this:
var sizes = [{
"oldName": "10",
"newName": "10_1",
"sizeExistsInDest": false,
"sizeExistsInSrc": false,
"filesInSrc": [],
"dirsInDest": [],
"filesInDest": [],
"filesToCopy": []
}.....]; //ten or so elements
Then I have two functions I'm trying to call in sequence. Like when one is complete, then
, call the next.
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;
};
When I call
var srcFiles = getFileData()
a console print of srcFiles
shows the modified sizes
array correctly. However when adding a then
.
var srcFiles = getFileData().then(getSrcFiles);
I think I should see files added to the sizes
array, I get the error I specified above. I'd like to chain a couple more then
s, but I cannot seem to modify my array more than once.