title: How the while loop works with promise and callback function in Nodejs?
body: <p>This is my first time to write while loop with Promise and callback. I don't know why it leads to infinite loop. How to fix it?</p>
  <pre><code>async function getResult(){
      return new Promise((resolve, reject) =&gt; {
          let params =&quot;some input and setting&quot;;
          let next = &quot;hasNext&quot;;
          let array = [];
          let error = null;
          while(next !== null){
              checkNext(params, function(err,data) { //checkNext is a function to return the current list and check wether there has the next list
                  if(err){
                      next = null;
                      error = err;
                  }else{
                      next = data.hasNext; // if there is not next list, data.hasNext = null
                      array = array.concat(data.array); // data.array return the current list
                  }
              });
          }
          if(error !== null){
              reject(error);
          }else{
              resolve(array); // I want to return all lists
          }
         
      });
  }
  </code></pre>