title: How to use conditions in while loop in Node JS which has a asynchronous query function with loop conditions modified only in callback functions?
body: <p>In my nodejs program, the while loop has conditions that can be modified by asynchronous function inside it. But, as the function is asynchronous, the program is going in infinite loop. How to go around this?  </p>
  
  <pre><code>let unique = true
  while(unique)
  {
      console.log("Inside Loop")
      let id = random.generate(5)
      let u_query = "Insert into table_db (id, name) values ("+db.escape(id)+", "+db.escape(name)+")";
      db.query(u_query, (error, results, fields) =&gt; {
          console.log("Inside Query")
          if(error)
          {
              if(error.code !== 'ER_DUP_ENTRY')
                  throw error
          }
          else
          {
              unique = false // to stop execution
              res.send("Shorted URL : "+newURL)
              // break is giving error
              //SyntaxError: Illegal break statement
          }
      })
      // To check if loop is stopping and asynch function is causing infinite loop
      // I used unique = false here
  }
  </code></pre>
  
  <p>Thanks!</p>