Q:

how to use defer and deferred.resolve in the same function where I used Q.all?

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;

}

A:

Pay attention to the @TODO portion. 
    /** iteratively calculate pagesize of the new incoming files **/
function filterNonConformingPageSize(files) {
    // @TODO Create a Deferred
    var deferred = Q.defer();

    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
    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++) {
            var filename = files[i].name;
            var newFileSize = pageSizesOfNewFiles.getByKey(filename);
            if (newFileSize.size == majorityPageSize) {
                console.log(filename + "is right size");
                console.log(newFileSize.size);
                filesWithOkaySize.push(files[i]);
            } else {
                console.log(filename + "is wrong size");
                filesWithWrongSize.push(files[i]);
            }
        }
        test['wrongsize'] = filesWithWrongSize;
        test['okaysize'] = filesWithOkaySize;
    // @TODO add the resolve        
        deferred.resolve(test); 
    });
    // @TODO return the promise part. 
    return deferred.promise;

}

when you call the function, do it like this
 filterNonConformingPageSize(filterDupeNameResult['okaynames']).then(function(result){
 // result will be the expected result
     // at the end of running filterNonConformingPageSize
 });

