I am having a hard time figuring out how promises work. I have the following code that I am executing iterate through an array.
runOpts.automationResults.reduce(function (p, val) {
        return p.then(function () {
              return  pd.run(val);
});
}, Promise.resolve()).then(function (rallyResults) {
     console.log('returned from method');
     console.log('rallyResults ', rallyResults);
}, function (err) {
     console.log('error in reduce', err);
});It is calling the following method
ProcessDatabase.prototype.run = function (options) {
    return new Promise((resolve, reject) => {
        var dal = new db();
        var resultsArray = {
            results: [],
            options: [],
            testSet: [],
            testIteration: '',
            multipleSets: 0,
            totalCount: 0
        }
        resultsArray.options = options;
        dal.getAutomationResultsByBuild(options.Build).then((results) => {
            resultsArray.results = results;
            resultsArray.totalCount = results.data.length;
            resolve(resultsArray);
        })
    }).then(function (resultsArray) {
        var results = [];
        //console.log('resultsArray', resultsArray);
        //console.log(`Starting Rally publishing for Build '${resultsArray.options.Build}'...`)
        if (resultsArray.options.Executiontype == 'ci') {
            rallyApi.autoDiscoverTestSets().then((sets) => {
                resultsArray.testSet = sets.sets;
                resultsArray.testIteration = sets.iteration;
                resultsArray.multipleSets = sets.sets.length > 1;
                results.push(resultsArray);
                console.log('results', results);
            }, (err) => { reject(err); });
            // totalResults = totalResults + results.data.length;
        } else {
            rallyApi.addTestSet('Automation: ' + resultsArray.options.type + ' - ' + resultsArray.options.build, config.get('rally.projects.testing.ref'), null, resultsArray.options.SessionUser).then((resp) => {
                //console.log(resp);
                console.log('New test set ' + resp.FormattedID + ' created.');
                resultsArray.multipleSets = 0;
                resultsArray.testSet = resp.FormattedID;
                //console.log('resultsArray', resultsArray);
                results.push(resultsArray);
                console.log('results', results);
            }, (err) => {
                console.log(err);
            });
        }
        console.log('results', results);
        return Promise.all(results);
    })
}I have tried several different iterations of what I currently have and it always goes back to the calling .reduce before the results are complete in the ProcessDatabase.prototype.run method.
I have put console logs in and this is what I get. I can see that the final results array has all the information I need. I just haven't been able to figure out how to get it passed back to the calling .reduce.
---These are actually from the .reduce and are written to the log before the results in the method called
returned from method 
rallyResults  [] 
**----These are from the ProcessDatabase.prototype.run method called**
**This is the contents of the "results" set the first iteration through**
-- Found 2 test sets.
results [ { results: { _success: true, _message: '', _data: [Array] },
    options:
     { Executiontype: 'ci',
       Build: 'test_030518_103956',
       Environment: 'dev',
       SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' },
    testSet: [ [Object], [Object] ],
    testIteration: 'https://rally1.rallydev.com/slm/webservice/v2.0/iteration/184203152680',
    multipleSets: true,
    totalCount: 4 } ]
New test set TS2969 created.
**This is the contents of the "result" set after the second iteration 
through and what I trying to get passed back to the calling .reduce.**
results [ { results: { _success: true, _message: '', _data: [Array] },
    options:
     { Executiontype: 'ci',
       Build: 'test_030518_103956',
       Environment: 'dev',
       SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' },
    testSet: [ [Object], [Object] ],
    testIteration: 'https://rally1.rallydev.com/slm/webservice/v2.0/iteration/184203152680',
    multipleSets: true,
    totalCount: 4 },
  { results: { _success: true, _message: '', _data: [Array] },
    options:
     { Executiontype: 'regression',
       Build: 'test_030518_110447',
       Environment: 'dev',
       SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' },
    testSet: 'TS2969',
    testIteration: '',
    multipleSets: 0,
    totalCount: 6 } ]
    Any help would be GREATLY appreciated. Thanks Christine
 
    