bluray
bluray

Reputation: 1953

Angular http promise and recursion

Hello I have three tables with linked records (record in next table has link on record in previous table). Now I would like to remove record from a table with his children in next tables. I wanted to use recursion, but I have problem with promise. Here is my code:

    $scope.removeItem = function (table, itemId) {
                var nextTableIndex = table.index + 1;
                dbService.remove(table, [{
                    by: table.parentColumn == undefined ? "ID" : table.parentColumn, values: itemId
                }]); 


                while (nextTableIndex < config.tables.length) {
                    var nextTable = config.tables[nextTableIndex];
                    dbService.getTableItems(nextTable.id, ["ID"], [{ by: nextTable.parentColumn, values: itemId }])
                      .then(function (data) {
                           var ids = select(data, "ID"); //return array with all ids ([1,2,3])
                           $scope.removeItem(config.tables[table.index + 1], ids);
                       });

                    nextTableIndex++;
                }
            }

dbService.getTableItems = function(tableId, columns, where){ //return promise
  return $http.get(createUrl(tableId, columns, where));
}

Removing only from first table and last table. In recursion use last nextTable because first it performed whole while and than it performed "then" function. I need first load data from database and than call next code.

It is a possibility how to load data immediately? Something like: dbService.getTableItems(...).toArray()

Thank you

Upvotes: 0

Views: 45

Answers (1)

Leguest
Leguest

Reputation: 2137

I am not clearly understand you, but you could try this

JS

$scope.removeItemRecursivly = function () {

        var nextTableIndex = 0;

        var currentTable = config.tables[nextTableIndex];

        dbService.remove(currentTable, [{
                        by: currentTable.parentColumn == undefined ? "ID" : currentTable.parentColumn, values: itemId
                    }]); 


        var removePromises = [];           

        function removeItem(){

            if(nextTableIndex < config.tables.length) {

                return new Promise(function(resolve, reject){

                    var nextTable = config.tables[nextTableIndex];
                    nextTableIndex = nextTableIndex + 1;

                    dbService.getTableItems(nextTable.id, ["ID"], [{ by: nextTable. parentColumn, values: itemId }])
                        .then(function (data) {

                            removePromises.push(removeItem())

                            resolve('Finish with table with index: ' + (nextTableIndex - 1) );


                        });


                })
            }

        }

        removePromises.push(removeItem())

        Promise.all(removePromises).then(function(){
                console.log('finish?')
        })

    }

Upvotes: 1

Related Questions