The requirement is that I used Q.js feature called Q.all inside a javascript function that needs to return a result provided the Q.all finishes successfully.
My objective is to return an object with two properties called wrongsize and okaysize.
both properties are javascript Array.
The problem is I am not sure how to include the Q.defer function with Q.all in the same function.
function filterNonConformingPageSize(files) {
var addToWhichGlobalVariable = "pageSizesOfNewFiles";
promises = [];
for (var i=0; i<files.length; i++) {
promises.push(
getSizeSettingsFromPage(files[i], addToWhichGlobalVariable, calculateRatio)
);
}
// *** sort out the files after all promises have been resolved
var resultObj = Q.all(promises).then(function() {
var test = new Object();
var filesWithOkaySize = new Array();
var filesWithWrongSize = new Array();
for (var i=0; i<files.length; i++) {
if (pageSizesOfNewFiles[i].size == majorityPageSize) {
filesWithOkaySize.push(files[i]);
} else {
filesWithWrongSize.push(files[i].name);
}
}
test['wrongsize'] = filesWithWrongSize;
test['okaysize'] = filesWithOkaySize;
return test;
});
return resultObj;
}
Pay attention to the @TODO portion.
when you call the function, do it like this