Rajan Prasad
Rajan Prasad

Reputation: 1679

Azure Logic Apps Foreach Loop Reduce to An Array

We have created a Logic App which iterates over some elements of an array in the input json, and then appends the output of each iteration to another array.

Now, if we are doing parallel execution of the foreach loop, is there a thread safe way to create the output array from the loop?

Upvotes: 0

Views: 1509

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8244

In general, Thread safety can be attained either by applying a condition or using the same array twice for parallel processing. In Logic Apps, If the parallel processes have 2 for each loop, then you don't have to think about thread safety.

enter image description here

This takes the same array as 2 separate arrays.

enter image description here

Below is the code-view of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Compose": {
                        "inputs": "@items('For_each')",
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@variables('Array')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "For_each_2": {
                "actions": {
                    "Compose_2": {
                        "inputs": "@items('For_each_2')",
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@variables('Array')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array",
                            "type": "array",
                            "value": [
                                1,
                                2,
                                3,
                                4,
                                5
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Upvotes: 0

Related Questions