Please help me to due with Promise inside Promise!
My PROVIDERS function do:
-
Get Questions from DBCALCULATOR (Promise)
-
With each Question, I checked if it is a multiple_choice question
-
If it is a multiple_choice question, call a SQL to get choices from DBANSWER_CHOICE (Promise again) and push all results to that question.
If not, push that question to formulars[]
But the problem is the getQuestions() function is not return a final formulars[], it’s always return undefined
getQuestions(calculatorIDENTIFIER): Observable<any>{
let sqlQueryQuestion = `SELECT
*
FROM
DBQUESTION
WHERE
CALCULATOR_ID = (
SELECT
_id
FROM
DBCALCULATOR
WHERE
IDENTIFIER = '${calculatorIDENTIFIER}'
)
ORDER BY
ORDERED_ID ASC;`
let sqlQueryAnswerChoice = `
SELECT * FROM DBANSWER_CHOICE WHERE QUESTION_ID=_QUESTION_ID_
`
let formulars=[];
let db = this.database
return Observable.fromPromise(
db.executeSql(sqlQueryQuestion, {}).then(re=>{
if(re.rows.length>0){
let cInd = -1
let checkTungCauHoi = function(){
cInd++
if (cInd < re.rows.length) {
let cauHoiNay = re.rows.item(cInd)
/* get AnswerChoice */
if( cauHoiNay.TYPE == "multiple_choice" ){
db.executeSql(sqlQueryAnswerChoice.replace(/_QUESTION_ID_/, cauHoiNay._id), {}).then(re2=>{
if(re2.rows.length>0){
cauHoiNay.choices=[]
for (var ind2 = 0; ind2 < re2.rows.length; ind2++) {
cauHoiNay.choices.push(re2.rows.item(ind2))
}
formulars.push(cauHoiNay)
checkTungCauHoi()
}
})
/* Else not multiple_choice thì push và gọi hàm tiếp */
} else {
formulars.push(cauHoiNay)
checkTungCauHoi()
}
} else {
return formulars
}
}
checkTungCauHoi()
}
})
)
}