Friday 14 April 2017

Promise.map function .then() block getting null before nested promise resolves

I'm kicking off a nested promise mapping and seeing the outer .then() block print a null result before the resolve in the function is called.

I feel like I must be messing up the syntax somehow. I've made this stripped down example:

const Promise = require('bluebird');
const topArray = [
    {
        outerVal1: 1,
        innerArray: [
            {
                innerVal1: 1,
                innerVal2: 2
            },
            {
                innerVal3: 3,
                innerVal4: 4
            }
        ]
    },
    {
        outerVal2: 2,
        innerArray: [
            {
                innerVal5: 5,
                innerVal6: 6
            },
            {
                innerVal7: 7,
                innerVal8: 8
            }
        ]
    },
]


promiseWithoutDelay = function(innerObject){
    return new Promise(function(resolve, reject){
        setTimeout(function (innerObject) {
            return resolve("noDelay");
        }, 0);
    })
}
promiseWithDelay = function(innerObject){
    return new Promise(function(resolve, reject){
        setTimeout(function (innerObject) {
            return resolve("delay");
        }, 3000);
    })
}

test1 = function(){

    return new Promise(function(resolve, reject){

        let newArray = [];
        let newArrayIndex = 0;

        Promise.map(topArray, function(outerObject){

            let innerArray = outerObject.innerArray;

            Promise.map(innerArray, function(innerObject){

                Promise.all([
                    promiseWithoutDelay(innerObject),
                    promiseWithDelay(innerObject)
                ])
                    .then(function(promiseResults){
                        console.log("setting value of newArray index " + newArrayIndex +
                            " to ", {result1: promiseResults[1], result2: promiseResults[2]});

                        newArray[newArrayIndex++] = {result1: promiseResults[1], result2: promiseResults[2]}
                        console.log("returning inner newArray: ", newArray );

                        return resolve(newArray);
                    })
            })
                .then(function () {
                    console.log("returning outer newArray: ", newArray );
                    return resolve(newArray);

                })

        })
            .then(function(){
                console.log("returning resolve(newArray): ", newArray );
                return resolve(newArray);

            })
    })
}

test1().then(function(newArray){
    console.log( "calling function received newArray : ", newArray);
});

What I'm trying to do is loop over an outer array that has some values that I need.
These values include a nested inner array that I must also loop over to get some values.
In the inner loop I pass the outer and inner values to promise functions in a Promise.all.
When the promise functions resolve they get assigned to a return object.
It seems to be working fine except for one of the promise functions sometimes has a delay as it's doing some calculations.
When this happens it is left out of the return value because it hasn't resolved yet.

Shouldn't it wait until the inner loop with Promise.all resolves before it returns from the outer loop?

Can you point me in the right direction?



via GForce

No comments:

Post a Comment