1

In getNestedFlags I'm adding values to finalJson array. Then after console.log I can see entire finalJson array with values but resolve returns an empty array. Do you know how to achive the same result in resolve as in console.log?

getScheduledFlags: (idTc, idScheduled) => {
    return new Promise((resolve, reject) => {

        var getNestedFlags = (array1, array2, res) => {
            array1.forEach(item => {
                var childrenItems = res.filter(item1 => item1.id_parent == item.id)
                if(childrenItems.length > 1){
                    var childrens = []
                    getNestedFlags(childrenItems, childrens, res)
                    array2[item.name] = childrens
                } else {
                    array2[item.name] = item.value
                }
            })
        }

        _tc.get(idTc).then(result => {
            var flags = result.flags
            var bases = result.bases

            _featureFlags.getAll().then(allFlags => {
                tcScheduled= _featureFlags.getFinalTcFlags(result)
                res = _this.replaceFlags(allFlags, tcScheduled.flags)
                parentFlags = res.filter(item => item.id_parent == 0)

                var finalJson = []

                getNestedFlags(parentFlags, finalJson, res)

                console.log(finalJson)
                resolve(finalJson)
            })
        })
    })

},
AKZB
  • 157
  • 1
  • 12
  • 2
    could you add more of your code here? – mbehzad Sep 12 '17 at 12:13
  • can u show resolve function here? so that we can see and solve ur problem – Sindhoor Sep 12 '17 at 12:15
  • @user2520818 yes – AKZB Sep 12 '17 at 12:19
  • `_tc.get` is your only async function right? and is the callback only called once? have you tried to resolve with some fixed array and see if maybe the promise resolve works correct but you have a bug reading the result later on? – mbehzad Sep 12 '17 at 12:24
  • avoid the [promise constructor anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – Jaromanda X Sep 12 '17 at 12:30
  • @user2520818 - `_featureFlags.getAll()` looks asynchronous too – Jaromanda X Sep 12 '17 at 12:36
  • @user2520818 as Jaromanda X wrote, _featureFlags.getAll() is also async – AKZB Sep 12 '17 at 12:38
  • `resolve returns an empty array` - the "resolve" function doesn't actually return anything - where in your code do you claim `finalJson` becomes an empty array? – Jaromanda X Sep 12 '17 at 12:39
  • the thing is, this code looks like it's not complete in the sense of showing everything that may be needed to answer you ... those two vars, `flags` and `bases` are never used - so, looks suspect to me – Jaromanda X Sep 12 '17 at 12:40
  • 1
    note: your code (removing the anti-pattern) could be re-written like https://jsfiddle.net/25azokjo/ (not an answer, so not posting one) – Jaromanda X Sep 12 '17 at 12:42
  • @JaromandaX big thanks for the example in jsfiddle! about your question above - i'm retriving resolve in router function so i can see it as a response from request – AKZB Sep 12 '17 at 12:49

2 Answers2

0

The code which you have posted must work as array is passed by reference rather than value in javascript. Check both the console.log() in the code before and after the call to resolve() function.

var finalJson = []
var parentFlags ='';
var res ='';
getNestedFlags(parentFlags, finalJson, res);

console.log(finalJson);

resolve(finalJson);

console.log(finalJson);

function getNestedFlags(parentFlags, finalJson, res){
  finalJson.push(1);  
  finalJson.push(2);
  finalJson.push(3);
}

function resolve(finalJson){
   finalJson.push(4);
   finalJson.push(5);
}
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Initializing array by [] was my problem. Now with {} everything is fine.

AKZB
  • 157
  • 1
  • 12