title: execute DAG like operations in scala Future
body: <p>I am working on use case where in I have to execute inter-dependent operations (defined as a Directed Acyclic Graph) using scala Future. Basically every operation (say node of DAG) will be executed in a Future and subsequent dependent nodes will be triggered (they should be in a Future too) once the current node Future completes. This will go on until every node has finished processing or one of them fails. So far I have (minimal code):</p>
  
  <pre><code>def run(node: Node, result: Result): Unit = {
    val f: Future[(Node, Result)] = Future {
      // process current Node 
      ...
    }
  
    f onComplete {
      case Success(x) =&gt;
        val n = x._1 // Current Node
        val r = x._2 // Result of current Node
        if (!n.isLeaf()) {
          n.children.foreach { z =&gt;
            run(z, r)
          }
        } 
      case Failure(e) =&gt; throw e
    }
  }
  </code></pre>
  
  <p>Is this correct way to tackle this problem (Calling another Future in a callback)? Again I don't have proper way stop other running future once one of the node fails processing.</p>
  
  <p>Can this be solved using Future composition? If so, how can I achieve that?</p>
  
  <p>Thanks,<br>
  Pravin</p>