title: How to make parallel calls to the same function in stage in Jenkins Declarative Pipeline
body: <p>What is the correct way to execute a function in a declarative pipeline in parallel?</p>
  
  <p>Several posts on this forum are suggesting that you build an array, and pass the array to 'parallel' or pass a function to 'parallel'</p>
  
  <p><a href="https://stackoverflow.com/questions/46894308/is-it-possible-to-create-parallel-jenkins-declarative-pipeline-stages-in-a-loop">Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/43645459/simple-parallel-execution-in-jenkins-for-an-array">Simple parallel execution in Jenkins for an array</a></p>
  
  <p>I have made several attempts to get it to work but it always runs in serial</p>
  
  <p>I think that the problem is that the function is being evaluated while I am building the array, even before I reach the 'parallel' step in the stage.</p>
  
  <p>I wasn't able to arrive at a solution from the official documentation.</p>
  
  <p><a href="https://jenkins.io/blog/2017/09/25/declarative-1/" rel="nofollow noreferrer">https://jenkins.io/blog/2017/09/25/declarative-1/</a></p>
  
  <p>Here is how I would like it to work, but is it possible?</p>
  
  <pre><code>    pipeline {
  
          agent {
              label "l1" &amp;&amp; "l2"
          }
  
          stages {
  
              stage ('Prepare Data') {
                  // Some code that creates that data object
                  // data is an array of maps
                  data
              }
  
              stage ('Build') {
                  script {
                      def stepsForParallel = [:]
                      data.each { d -&gt;
                          // name is a key in the data map
                          stepsForParallel["${d['name']}"] = {
                              node {
                                  stage("${d['name']}") {
                                      stepsForParallel[execDownStreamJob("DownStreamJobName", d)] = {
                                          println("Executing DownstreamJob")
                                      }
                                  }
                              }
                          }
                      }
                      parallel stepsForParallel
                  }
              }
  
          }
      }
  }
  </code></pre>
  
  <p>The function below isn't really in the scope of this question, but I've added it in case it offers some value. It is explained best in this post: <a href="https://stackoverflow.com/a/42248825/5006720">https://stackoverflow.com/a/42248825/5006720</a></p>
  
  <pre><code>// job is a string
  // jobParameters is a hashmap
  def execDownStreamJob(job, jobParameters) {
  
      // Some parsing takes place on hashMap and creates the p object 
      // 'p' is passed to the 'build job' function as a parameter
  
      def downStreamJobResult = build job: job, parameters: p, propagate: false
  
      // Some try/catch logic
  
  }
  
  </code></pre>