0

So I have a forEach with Promises.all block and then inside the callback for this 2 promises productsController.addSizeQuantity and productsController.addProductsQuantity I have productsController.disableProduct or productsController.enableProduct or others (depends on conditions).

What I need to achieve is run some code after foreach will be finished and all promises like productsController.disableProduct or others will be resolved.

Is there any way to do that?

data.forEach(function (element) {
    var rowArray = element.split(';');

    var torgsoftID = rowArray[0];
    var quantity = rowArray[12];

    Promise.all(
        [
            productsController.addSizeQuantity(torgsoftID, quantity),
            productsController.addProductsQuantity(torgsoftID, quantity)
        ]).then(() => {
        // Disable or enable product/size based on torgsoft information
        productsController.getProductByTorgsoftID(torgsoftID)
            .then(result => {
                if (result.length) {
                    var isEnabled = result[0].enabled;

                    if (!!isEnabled && quantity < 1) {
                        productsController.disableProduct(torgsoftID);
                    } else {
                        productsController.enableProduct(torgsoftID);
                    }
                } else {
                    productsController.getSizeByTorgsoftID(torgsoftID)
                        .then(result => {
                            if (result.length) {
                                if (quantity < 1) {
                                    productsController.disableSize(torgsoftID);
                                } else {
                                    productsController.enableSize(torgsoftID);
                                }
                            }
                        });

                }
            })
    });
})
Andreas
  • 21,535
  • 7
  • 47
  • 56
Alexander Seredenko
  • 799
  • 3
  • 9
  • 26

0 Answers0