mardi 1 septembre 2015

Angular JS Loop Promise

I have a situation here. I have a promise function which runs in such a way

  1. My promise function "getQuestion" calls another promise function DB.query which fetches result (Main_Question) from mysqlite database.

  2. After I get result of DB.query, I call getAnswer function which is also a promise function which fetches answer of that question

  3. Now the Main_Question may have N number of sub-questions. So I again run DB.query which fetch all Sub_Questions.

  4. Now I have to fetch answers of these Sub_Questions. Obviosuly it can only be done via loop. So I run a for loop which iterates through each element of Sub_Question to get its answer

Now the problem is with step number 4, the loop runs good but doesn't return a promise resonse therefore I sometimes get incorrect values for each sub-question answers.

Here is my code

self.getQuestion = function(questionNumber, sectionId) {

var deferred = $q.defer();

var question = {};
question.hasCounter = false;
question.hasDetail = false;
question.hasSubSection = false;

console.log('SELECT * FROM question WHERE question_type = 1 AND question_number = '+ questionNumber +' AND section_id = ' + sectionId);
DB.query('SELECT * FROM question WHERE question_type = 1 AND question_number = ? AND section_id = ?', [questionNumber, sectionId])
.then(function(result){
  console.log(result);
  question.main = DB.fetch(result);

  console.log(question.main);
  Answer.getAnswer(question.main.question_id).then(function(answer) {

    question.main.answer = answer;

    //Check if this question has any counter question

    console.log('SELECT * FROM question WHERE question_type = 2 AND parent_id = ' + question.main.question_id);
    DB.query('SELECT * FROM question WHERE question_type = 2 AND parent_id = ?', [question.main.question_id])
    .then(function(result){
      console.log(result);
      if(result.rows.length == 0)
      {
        question.hasCounter = false;

        //Check if this question still has any subsection (s)
        console.log('SELECT * FROM question WHERE section_id = '+ sectionId +' AND question_number = '+ question.main.question_number +' AND question_number_section != ""');
        DB.query('SELECT * FROM question WHERE section_id = ? AND question_number = ? AND question_number_section != ""', [sectionId, question.main.question_number])
        .then(function(result){
          if(result.rows.length == 0)
          {
            question.hasSubSection = false;
            deferred.resolve(question);
          }
          else
          {
            question.hasSubSection = true;
            question.sub = DB.fetchAll(result);
            console.log(question.sub);

            for (var i in question.sub) 
            {
              console.log("i+"+i);
              (function(j) 
              {
                console.log("j+"+j);
                question.sub[j].answer = {};
                Answer.getAnswer(question.sub[j].question_id).then(function(res) {
                  question.sub[j].answer = res;

                  if(j == (question.sub.length-1))
                  {
                    deferred.resolve(question);
                  }
                })
              })(i);
            };
          }
        });
      }
      else
      {
        question.hasCounter = true;
        question.counter = DB.fetch(result);
        Answer.getAnswer(question.counter.question_id).then(function(answer) {
          question.counter.answer = answer;
        });

        //Get detail question
        DB.query('SELECT * FROM question WHERE question_type = 3 AND parent_id = ?', [question.counter.question_id])
        .then(function(result){
          question.detail = DB.fetchAll(result);

          question.hasDetail = true;

          for (var j in question.detail) 
          {
            (function(i)
            {
              question.detail[i].answer = {};
              console.log(question.detail[i].question_id);
              Answer.getDetailAnswer(question.detail[i].question_id, i).then(function(res) {

                console.log(res);
                question.detail[i].answer = res;

                if(j == (question.detail.length-1))
                {
                  deferred.resolve(question);
                }
              })
            })(j);
          };
        });
      }
    });
  });
});

return deferred.promise;
  };


self.getAnswer = function(questionId) {

var deferred = $q.defer();
DB.query('SELECT * FROM answer WHERE question_id = ?', [questionId])
.then(function(result){

  answer = DB.fetch(result);

  if(answer && answer.answer_type == 3)
  {
    deferred.resolve(answer);
  }

  //Get Answer Option
  if(answer)
  {
    DB.query('SELECT * FROM answer_option WHERE answer_id = ? ORDER BY option_code', [answer.answer_id])
    .then(function(result){

      answer.answerOptions = DB.fetchAll(result);

      deferred.resolve(answer);
    });
  }
});

return deferred.promise;
 };

  self.getDetailAnswer = function(questionId, index) {

var deferred = $q.defer();
var answer = [];

DB.query('SELECT * FROM answer WHERE question_id = ?', [questionId])
.then(function(result){

  answer[index] = DB.fetch(result);

  if(answer[index] && answer[index].answer_type == 3)
  {
    console.log("Resolved: " + answer[index].answer_id);
    deferred.resolve(answer[index]);
  }

  //Get Answer Option
  if(answer[index])
  {
    DB.query('SELECT * FROM answer_option WHERE answer_id = ? ORDER by option_code', [answer[index].answer_id])
    .then(function(result){

      //console.log(answer.answer_id + " / " + questionId);

      answer[index].answerOptions = DB.fetchAll(result);

      deferred.resolve(answer[index]);
      console.log("Resolved 2: " + answer[index].answer_id + " / " + questionId);
    });
  }
});

return deferred.promise;
 };

Aucun commentaire:

Enregistrer un commentaire