异步/等待功能不等待
Async / Await function not awaiting
这里我尝试创建一个函数,使用 MVC 模式从数据库中检索一些数据。
查看
getQuestionsData (treatmentId) {
console.log(1)
controller.getTreatmentQuestions(treatmentId)
.then(questions => {
console.log(10)
this.questions = questions // variable is updated
})
},
控制器
getTreatmentQuestions: async (treatmentTypeId) => {
console.log(2)
// first data fetch from db
const questions = await model.getQuestion(treatmentTypeId)
console.log(3)
// iterate over each result record
questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
})
return questions
}
型号
static async getQuestionChoices (questionId) {
console.log(6)
const answers = await db.choiceAnswers
.where('questionId').equals(intId)
.with({
selectChoices: 'choiceAnswersChoices'
})
console.log(7)
return answers.map(answer => {
console.log(8)
answer.choices = answer.selectChoices
return answer
})
}
我希望在控制台中按以下顺序读取数字:1、2、3、4、5、6、7、8、9、10。
相反,控制台中打印的序列是:1、2、3、4、5、6、10、7、8、9。
这意味着模型的函数 "getQuestionChoices" 没有等待 return 语句。
我该如何解决这个问题?
谢谢
这个:
questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
})
Returns promise 数组,结果未分配到任何地方
修复:
questions = await Promise.all(questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
}))
这里我尝试创建一个函数,使用 MVC 模式从数据库中检索一些数据。
查看
getQuestionsData (treatmentId) {
console.log(1)
controller.getTreatmentQuestions(treatmentId)
.then(questions => {
console.log(10)
this.questions = questions // variable is updated
})
},
控制器
getTreatmentQuestions: async (treatmentTypeId) => {
console.log(2)
// first data fetch from db
const questions = await model.getQuestion(treatmentTypeId)
console.log(3)
// iterate over each result record
questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
})
return questions
}
型号
static async getQuestionChoices (questionId) {
console.log(6)
const answers = await db.choiceAnswers
.where('questionId').equals(intId)
.with({
selectChoices: 'choiceAnswersChoices'
})
console.log(7)
return answers.map(answer => {
console.log(8)
answer.choices = answer.selectChoices
return answer
})
}
我希望在控制台中按以下顺序读取数字:1、2、3、4、5、6、7、8、9、10。 相反,控制台中打印的序列是:1、2、3、4、5、6、10、7、8、9。
这意味着模型的函数 "getQuestionChoices" 没有等待 return 语句。
我该如何解决这个问题?
谢谢
这个:
questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
})
Returns promise 数组,结果未分配到任何地方
修复:
questions = await Promise.all(questions.map(async question => {
console.log(4)
if (question.answerType === 'select') {
console.log(5)
// second data fetch from db
const answerWithChoices = await model.getQuestionChoices(question.id)
console.log(9)
// update question object with fetched data
question = Object.assign({}, question, {
choices: answerWithChoices.choices
})
}
return question
}))