Search code examples
javascriptnestedes6-promise

How to get data from lower nested function to the higher nested function?


I have a problem to get data from lower nested function to the higher nested function, lets suppose required data is async I did try callbacks to no avail, promises do not give me proper result also - am I doing something wrong? I'm new in node.js environment.

Code:

var array = [1,2,3,4,5,6,7,8,9,10];

for (var i = 0; i < array.length; i++)
{
    if (array[i] == '3'){
        function tfunc1(){
            function tfunc2(){
                return new Promise(function(resolve, reject){
                    setTimeout(function(){ // lets suppose that there is some async work here
                        var tsomeData = 'TsomeData';
                        resolve(tsomeData);
                        reject(console.log('tfunc4 ERROR'));
                    }, 2000);
                });
            }
                    // promises:
                    tfunc2().then(function(tsomeData)
                    {
                        return afunc2(tsomeData);
                    }).then(function({tsomeData, asomeData})
                    {
                        return after(tsomeData, asomeData);
                    }).catch(function()
                    {
                        console.log(err);
                    });
        }
    }

    if (array[i] == '6'){
        function afunc1(){
            function afunc2(tsomeData){
                return new Promise(function(resolve, reject){
                    setTimeout(function(){ // lets suppose that there is some async work here
                        var asomeData = 'AsomeData';
                        resolve({tsomeData : tsomeData, asomeData : asomeData});
                        reject(console.log('afunc4 ERROR'));
                    }, 2000);
                });
            }
        }
    }

    // I need to work with tsomeData here - how to pass it here?
    // I need to work with asomeData here - how to pass it here?

    // tsomeData + asomeData mutating:
    function after(tsomeData, asomeData){
        return new Promise(function(resolve, reject){
            setTimeout(function(){ // lets suppose that there is some async work here
                var newData = tsomeData + asomeData;
                resolve(console.log(newData));
                reject(console.log('after ERROR'));
            }, 2000);
        });
    }
}

fiddle

Any idea how to resolve that?


Solution

  • To be honest, I have no idea what exactly you're trying to accomplish. The JSFiddle has quite a few mistakes and I have a hard time gauging the intentions.

    There is however a way to simplify this by moving those functions outside of the loop and using the Promise.all functionality, like this. It's probably not entirely what you're expecting but I'm hoping it gives you something to go by:

    var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    var array_length = array.length;
    var promises = [];
    console.clear();
    
    
    function tfunc1() {
      promises.push(tfunc2());
    }
    
    function afunc1() {
      promises.push(afunc2());
    }
    
    function tfunc2() {
      return new Promise((resolve, reject) => {
        setTimeout(time => resolve('Tsomedata'), 2000);
      });
    }
    
    function afunc2() {
      return new Promise((resolve, reject) => {
        setTimeout(time => resolve('Asomedata'), 1000);
      });
    }
    
    for (var i = 0; i < array_length; i++) {
      switch (array[i]) {
        case 3:
          tfunc1();
          break;
        case 6:
          afunc1();
          break;
        default:
          break;
      }
    }
    
    Promise.all(promises).then(data => {
      //both Tsomedata and Asomedata are available in the `data` array.
      console.log(data);
    });