title: DOJO Promise All sub function
body: <p>I'm having trouble figuring out how I could do sub functions and have a deferred function wait to call that before returning. </p>
  
  <p>I've taken a sample and modified it. It shows what i'd like to do.</p>
  
  <p>Basicly in the following example  bingRequest calls sub which just sets a value. I'd like the BingRequest to wait till that sub call is completed.  In my real world case that value effects what the bingRequest function would return. I've currently just got sub code stuck in the bingRequest but it makes for a very large function that would be cleaner if I could have the sub code split out. If I have to do it all combined I will but would like to be able to split some code out.</p>
  
  <p>Thanks for taking a look.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html &gt;
  &lt;head&gt;
  
      &lt;link rel="stylesheet" href="../../_static/js/dojo/../dijit/themes/claro/claro.css"&gt;
  
      &lt;script&gt;dojoConfig = { async: true, parseOnLoad: false }&lt;/script&gt;
      &lt;script src='//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js'&gt;&lt;/script&gt;
  
      &lt;script&gt;
          require(["dojo/promise/all", "dojo/Deferred", "dojo/dom", "dojo/on", "dojo/json", "dojo/domReady!"],
          function (all, Deferred, dom, on, JSON) {
              var y = "none"
              function googleRequest() {
                  var deferred = new Deferred();
                  setTimeout(function () {
                      deferred.resolve("foo");
                  }, 500);
                  return deferred.promise;
              }
  
              function bingRequest() {
                  var deferred = new Deferred();
                  setTimeout(function () {
                      deferred.resolve("bar");
                      sub();
                  }, 750);
                  return deferred.promise;
              }
              function sub() {
  
                  setTimeout(function () {
                   y = "some";
                  }, 750)
              }
  
  
              function baiduRequest() {
                  var deferred = new Deferred();
                  setTimeout(function () {
                      deferred.resolve("baz");
                  }, 1000);
                  return deferred.promise;
              }
  
              on(dom.byId("startButton"), "click", function () {
                  dom.byId("output").innerHTML = "Running...";
                  all([googleRequest(), bingRequest(), baiduRequest()]).then(function (results) {
                      dom.byId("output").innerHTML = JSON.stringify(results)+y;
                  });
              });
  
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body class="claro"&gt;
      &lt;h1&gt;Output:&lt;/h1&gt;
  &lt;pre id="output"&gt;&lt;/pre&gt;
  &lt;button type="button" id="startButton"&gt;Start&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>