title: One Promise from multiple files
body: <p>The example below shows a snippet of my application. The assumption is: some functions are executed after the document is loaded, others only after the POST response. On this momet, callbacks are placed in the array and executed after the request is completed. It all works as expected. I would like to change these callbacks to one Promise which would be fired just like now &quot;Update.endRequest()&quot;; I wonder if it is possible? Any suggestions are welcome. Or an idea if it can be done better.</p>
  <p>update.js</p>
  <pre><code>class UpdateClass {
      constructor() {
          this.callbacks = [];
      }
  
      success(callback) {
          this.callbacks.push(callback);
      }
  
      endRequest() {
          this.callbacks.forEach(callback =&gt; callback());
      }
  }
  
  const Update = new UpdateClass();
  export default Update;
  </code></pre>
  <p>file1.js</p>
  <pre><code>import Update from './update';
  
  export default class File1Class {
      constructor () {
          this.init();
      }
  
      init() {
          Update.success(this.update);
  
          exampleFunction();
      }
  
      update() {
          exampleFunctionAfterUpdate();
      }
  }
  
  function exampleFunction() {
      console.log('On document load 1');
  }
  
  function exampleFunctionAfterUpdate() {
      console.log('After Update 1');
  }
  </code></pre>
  <p>file2.js</p>
  <pre><code>import Update from './update';
  
  export default class File2Class {
      constructor () {
          this.init();
      }
  
      init() {
          Update.success(this.update);
  
          anotherExampleFunction();
      }
  
      update() {
          anotherExampleFunctionAfterUpdate();
      }
  }
  
  function anotherExampleFunction() {
      console.log('On document load 2');
  }
  
  function anotherExampleFunctionAfterUpdate() {
      console.log('After Update 2');
  }
  </code></pre>
  <p>app.js</p>
  <pre><code>import Update from './update';
  import File1Class from './file1';
  import File2Class from './file2';
  
  class AppClass {
      constructor () {
          this.init();
      }
      
      init() {
          new File1Class();
          new File2Class();
          
          triggerfunction();
      }
  }
  
  function triggerfunction() {
      const button = document.getElementById('trigger');
      
      button.addEventListener('click', () =&gt; {
          xhr.open('POST', 'someUrl', true);
          xhr.onload = () =&gt; {
              if (xhr.status === 200) {
                  Update.endRequest();
              }
          };
          xhr.send(someJsonData);
      });
  }
  
  window.app = new AppClass();
  </code></pre>