title: Promise does not resolve node.js
body: <p>I am trying to resolve some promises by the <code>sendRequest</code> function but it does not work.</p>
  
  <p>For example, if I evoke <code>sendRequest function</code> 4 times, all of the times, I can see the log printed in the console and then going to <code>resolve(data)</code>. But only 1 out of 4 times, the program reaches in  to <code>sendRequest.then()</code>.</p>
  
  <p>Here is the complete code of that file.</p>
  
  <h1>change-name.js</h1>
  
  <pre><code>var sendRequest = function(fileName){
      return new Promise(function (resolve,reject) {
          httpRequest(fileName).then(function (data) {
              try{
                  if(.....){
                      if(.....){
                          var lastIndex = ....;}
                      if(.....){
                          var lastIndex = ....;}
                      str = fileName.substring(0, lastIndex);
                      if(.........){
                          sendRequest(str);}
                      else{
                          reject("this is the end");}
                      }
                  else{
                      console.log("end result" + JSON.stringify(data,null,4));
                      resolve(data);
                      //===== THIS RESOLVE DOES NOT WORK WHILE THE LOG PRINTS THE DATA =====//
                      }
                  }
              catch(e){
                  resolve(data);
                  }
          }).catch(function (err) {
              reject(err);
          });
      });
  };
  </code></pre>
  
  <h1>server.js</h1>
  
  <p>this files calls the <code>sendRequest</code> function from the <code>change-name.js</code> and the then method is applied here for that function.</p>
  
  <pre><code>fs.readdir(path,(err,files)=&gt;{
      if(err){
          console.log(err);
          return;
          }
      for(i=0;i&lt;files.length;i++){
          sendRequest(files[i]).then(function (data) {
          console.log(data + "\n");
      }).catch(function(err){
          console.log("end Error is " + err + "\n");
      });
      console.log(files);
    }
  });
  </code></pre>
  
  <p>The github link is "<a href="https://github.com/abhikulshrestha22/movierator" rel="nofollow noreferrer">https://github.com/abhikulshrestha22/movierator</a>". </p>
  
  <p>Any help would be appreciated. Thanks </p>