title: Wait for nested JS promise to finish before resolving original promise
body: <p>I am new to Promises and I'm having a little trouble with the concept of waiting for a nested promise to finish all executions before resolving the original promise.</p>
  <p><strong>Original Code</strong></p>
  <pre><code>function getSomething(text) {
      return new Promise(function (resolve, reject) {
          getElse(text).then(function (items) {
              if (items.length !== 0) {
  
                  /* Stuff here */
  
                  getElseElse(box).then(function (moreItems) {
  
                      /* Stuff here */
  
                      return array;
                  }.then(function (array) {
                      var promise = new Promise(function (resolve, reject) {
                          /* anotherFunction() is asynchronous */
                          result = anotherFunction(array); &lt;-- Spot 1
                      });
                      promise.then(function () { });
                  });
  
                  return resolve(result); &lt;--- Spot 2
  
              }
              else {
                  return resolve(null);
              }
          });
      });
  };
  </code></pre>
  <p><strong>Updated Code</strong> - better but still not fully working like I want.</p>
  <pre><code>function getSomething(text) {
      return getElse(text).then(function (items) {
          if (items.length !== 0) {
  
              /* Stuff here */
  
              return getElseElse(box).then(function (moreItems) {
  
                  /* Stuff here */
  
                  return array;
              }).then(anotherFunction);
  
          } else {
              return null;
          }
      });
  }
  </code></pre>
  <p>Then, inside of the individual View page, I have this:</p>
  <pre><code>getSomething(text).then(function (result) {
      /* Do something here using result */
      /* But result is undefined and I think still pending */
  });
  </code></pre>
  <p>I have optimized the original function using Thomas's help but the original call inside the view still seems to be continuing before <code>result</code> is formulated.</p>
  <p>I want <code>getSomething()</code> to full complete and return <code>result</code> before I execute the code inside of the <code>.then</code> that is inside the view. So far, that is not being accomplished.</p>
  <p>I have found a couple posts that have pointed me in what I think is the correct direction of <code>Promise.all</code> but I can't really seem to get anywhere with that information so I was hoping someone could help explain it for my specific situation.</p>
  <p>The posts that have helped are:</p>
  <ol>
  <li><a href="https://stackoverflow.com/questions/41173220/promise-chain-not-waiting-for-promises-to-resolve-before-ending">Promise Chain not waiting for promises to resolve before ending</a></li>
  <li><a href="https://stackoverflow.com/questions/51529197/wait-until-nested-promises-resolve">Wait until nested promises resolve</a></li>
  <li><a href="https://stackoverflow.com/questions/43401703/wait-for-promises-inside-promise-all-to-finish-before-resolving-it">Wait for promises inside Promise.all to finish before resolving it</a></li>
  </ol>
  <h1>Solution</h1>
  <p>The original issue was solved by Thomas in the marked answer. The final problem was solved inside of <code>anotherFunction()</code> upon further inspection.</p>
  <p>Originally:</p>
  <pre><code>function anotherFunction(array) {
      return new Promise(function (resolve, reject) {
          return anotherGet(parcels).then(function (list) {
              /* Do stuff here without a return */
          });
      });
  }
  </code></pre>
  <p>Fixed Version:</p>
  <pre><code>function anotherFunction(array) {
      return anotherGet(parcels).then(function (list) {
          /* Do stuff here */
          return list;
      });
  }
  </code></pre>