title: Basic Javascript promise implementation attempt
body: <p>To gain better understanding of how promises work in Javascript I decided to give it a try and code basic implementation myself. </p>
  
  <p>Basically I want to implement Promises Object (I call it Aaa in my code) that takes function as an argument. This function can call resolve to <code>resolve</code> the promise, or reject to <code>reject</code> it. The basic implementation and usage is below. Not sure if the second argument is accepteable according to promise specs, but that's what I got so far.</p>
  
  <pre><code>Aaa=function(f,pause) { 
  
      console.log("ggg");
  
      var t=this;
      this.f=f;
      this.thens=[];
  
      this.resolve=function(g) {
  
          for(var i=0;i&lt;t.thens.length;i++)
          {
              // try/catch to be used later for dealing with exceptions
  
              try
              {
                  t.thens[i].f(g);
                  t.thens[i].resolve();
              }   
              catch(ex)
              {}
  
          }
      };  
  
      // to be implemented later
      this.reject=function(g) {};
  
      this.then=function(resolve,reject) {
  
          // i'm passing true for pause argument as we dont need to execute promise code just yet
          var nextPromise=new Aaa(resolve,true);
  
          this.thens.push(nextPromise);
  
          return nextPromise;
      }
  
  
      if(!pause)
          this.f(this.resolve,this.reject); 
  
  }
  
  
  var aaa=new Aaa(function(resolve,reject) {
  
      console.log("aaa");
  
      setTimeout(function() {
  
          console.log("fff");
          resolve("good");
  
      },2000);
  
      console.log("bbb");
  
  });
  </code></pre>
  
  <p>So now the promise can be created, called and resolved. Each <code>then</code> method will return new Aaa (Promise) so these can be chained. Now the code below uses promise created above and chains <code>then</code> callbacks. Each <code>then</code> returns new promise and in this case it seems to work fine:</p>
  
  <pre><code>aaa.then(function(res) {
  
      console.log("ccc");
      console.log(res);
  
  })
  .then(function(res) {
      console.log("ddd");
      console.log(res);
  },function(rej) {
      console.log("eee");
      console.log(rej);
  });
  </code></pre>
  
  <p>the output I'm getting is:</p>
  
  <pre><code>ggg
  aaa 
  bbb 
  ggg 
  ggg 
  fff 
  ccc 
  good 
  ddd 
  undefined 
  </code></pre>
  
  <p>The problem is however when one of the <code>then</code> calls returns a promise:</p>
  
  <pre><code>aaa.then(function(res) {
  
      console.log("ccc");
      console.log(res);
  
      // here we return the promise manually. then next then call where "ddd" is output should not be called UNTIL this promise is resolved. How to do that?
  
          return new Aaa(function(resolve,reject) {
  
          console.log("iii");
  
          setTimeout(function() {
          console.log("kkk");
              resolve("good2");
              // reject("bad");
  
          },2000);
  
          console.log("jjj");
  
      }).then(function (res) {
          console.log("lll");
  
          console.log(res);
      });
  
  })
  .then(function(res) {
      console.log("ddd");
      console.log(res);
  },function(rej) {
      console.log("eee");
      console.log(rej);
  });
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>ggg 
  aaa 
  bbb 
  ggg 
  ggg  
  fff  
  ccc  
  good  
  ggg  
  iii  
  jjj  
  ggg  
  ddd  
  undefined  
  kkk  
  lll  
  good2 
  </code></pre>
  
  <p>The call then where <code>ddd</code> is output should not be called UNTIL the returned promise we just added is resolved. </p>
  
  <p>How would that be best implemented? </p>