title: How to convert an asynchronous function into synchronous function using promise in Node.js?
body: <p>I have changed my code with async as suggested I am still not able to access the value and getting the error of Cannot read property '0' of undefined.The task_id is not accessible by the third function as its not created: </p>
  
  <pre><code> app.post('/api/add_task',function(req,res,error){
    var sql = "INSERT INTO `campus_ambassador`.`tasks` ( `task_type`, `task_name`, `details`, `social_media_handles`, `media`, `created_date`, `updated_date`) VALUES ('offline', 'golfed', 'ssss', '#soccer', 'sdfsd', '2012-12-26 00:00:00', '2012-12-26 00:00:00');"
    var sql1 = "select task_id from tasks where task_name='golfed'";
    var sql2 = "INSERT INTO `campus_ambassador`.`task_level` (`task_id`, `level_id`, `updated_on`, `created_on`) VALUES ("+"'"+res1[0].task_id+"'"+", '3', '2012-12-26 00:00:00', '2012-12-26 00:00:00')";
    var res1=''
  
    async.waterfall([
    function(callback){  
        db.select(sql,function(err){
            console.log("made the task");
            callback(null);
  
            })
  
        },
  
        function(callback){
    db.select(sql1,function(err,data){
                  res1 = data;
                  console.log(res1);
                  console.log(res1[0].task_id);
                  console.log("got the task id of the created task")
                  res.end();
                   callback(null,data);   
                      })
            },
      function(callback){
                db.select(sql2,function(err){
                console.log(res1[0].task_id)
              console.log("updated the task level table")
              callback(null);
            })
         }
  
   ],
      function(err,results){
          console.log(JSON.stringify(results));
      })
  })
  </code></pre>