Angular 异步函数 returns 未定义而不是 promise
Angular async function returns undefined instead of promise
我正在使用 Angular 控制器,它将上传到服务器的一个或多个调查中的不确定数量的问题添加到同名的现有调查中,这只是为了理解,上传、后台处理和returning响应完美无缺,这里真正的问题是刚刚上传新问题的特定部分,它具有以下功能:
//Will save next question
const next_question = function (response, this_survey, response_survey, sur_questions, question) {
deferred = $q.defer();
new_question = new QuestionsService();
//Does a bunch of stuff with question object using all those arguments,
//which is not relevant here
if (check_existing_question(new_question, sur_questions)) {
deferred.resolve();
}
else {
new_question.$save({
actsern: new_question.actsern
}).then(function () {
deferred.resolve();
}).catch(function () {
deferred.reject();
});
}
return deferred.promise;
};
// Save the questions synchronously (wait for a promise to resolve/reject before saving next question)
async function save_question(response, this_survey, response_survey, sur_questions) {
// I want to store all promises in an array and return for future error handling
promises = [];
for (const quest in response_survey) {
// promise is undefined in console.log!!
promise = await next_question(response, this_survey, response_survey, sur_questions, quest);
console.log(promise);
//Commented because if not returns error "promises.push is not a function"
//promises.push(promise);
}
//Still undefined
console.log(promise);
return promise;
//The above is how I managed to make it work but what I really want is:
//return promises;
}
const set_survey_questions = function(response, this_survey, response_survey){
//Never mind this, unrelated to my problem
let sur_questions = QuestionsService.get({
//this_survey is survey object that is being uploaded to, questions and surveys are linked through actsern
actsern: this_survey.actsern
});
sur_questions.$promise.then(function () {
promises = save_question(response, this_survey, response_survey, sur_questions);
$q.all(promises).then(function () {
console.log("Uploaded successfully all questions");
}).catch(function (reason) {
console.log(reason);
});
});
};
问题是我必须同步保存问题,在保存下一个之前等待一个到 resolve/reject,这就是我使用异步循环函数的原因。一切正常,问题在下单后上传,一切都按照预期的方式发送到服务器。问题是,我想从异步函数中获得 next_question()
的承诺,以便在将来处理它们的错误,并在使用 $q.all()
解决所有问题后做一些其他事情,但是问题是,据我所知 await
应该 return 一个承诺,但它只是 returns undefined
并且即使在循环完成后仍保持 undefined
所有的承诺,据说,解决。
我知道函数 next_question()
工作正常,因为如果直接调用它(在异步函数之外,直接从 set_survey_questions()
调用)它会保存问题并且 returns 只是承诺正如它应该的那样。
我对 angular 甚至 javascript 都不是很有经验,所以欢迎您提供任何帮助或改进。
As far as I know the await should return a promise but it just returns undefined
没有。您应该 将 一个承诺传递给 await
运算符,然后它将阻止异步函数的执行,直到承诺得到解决并且 return 承诺的结果值。
The problem is that I would like to get the promises of next_question() from the async function to deal with their errors in the future
这似乎不符合您对顺序保存的要求。没有多重承诺,一次只有一个活动。如果它们中的任何一个出错,await
将抛出异常并且您的循环将停止。
我认为你真的应该简化为
async function set_survey_questions(response, this_survey, response_survey) {
let sur_questions = QuestionsService.get({
actsern: this_survey.actsern
});
await sur_questions.$promise;
try {
for (const quest in response_survey) {
await next_question(response, this_survey, response_survey, sur_questions, quest);
}
console.log("Uploaded successfully all questions");
} catch (reason) {
console.log(reason);
}
}
我正在使用 Angular 控制器,它将上传到服务器的一个或多个调查中的不确定数量的问题添加到同名的现有调查中,这只是为了理解,上传、后台处理和returning响应完美无缺,这里真正的问题是刚刚上传新问题的特定部分,它具有以下功能:
//Will save next question
const next_question = function (response, this_survey, response_survey, sur_questions, question) {
deferred = $q.defer();
new_question = new QuestionsService();
//Does a bunch of stuff with question object using all those arguments,
//which is not relevant here
if (check_existing_question(new_question, sur_questions)) {
deferred.resolve();
}
else {
new_question.$save({
actsern: new_question.actsern
}).then(function () {
deferred.resolve();
}).catch(function () {
deferred.reject();
});
}
return deferred.promise;
};
// Save the questions synchronously (wait for a promise to resolve/reject before saving next question)
async function save_question(response, this_survey, response_survey, sur_questions) {
// I want to store all promises in an array and return for future error handling
promises = [];
for (const quest in response_survey) {
// promise is undefined in console.log!!
promise = await next_question(response, this_survey, response_survey, sur_questions, quest);
console.log(promise);
//Commented because if not returns error "promises.push is not a function"
//promises.push(promise);
}
//Still undefined
console.log(promise);
return promise;
//The above is how I managed to make it work but what I really want is:
//return promises;
}
const set_survey_questions = function(response, this_survey, response_survey){
//Never mind this, unrelated to my problem
let sur_questions = QuestionsService.get({
//this_survey is survey object that is being uploaded to, questions and surveys are linked through actsern
actsern: this_survey.actsern
});
sur_questions.$promise.then(function () {
promises = save_question(response, this_survey, response_survey, sur_questions);
$q.all(promises).then(function () {
console.log("Uploaded successfully all questions");
}).catch(function (reason) {
console.log(reason);
});
});
};
问题是我必须同步保存问题,在保存下一个之前等待一个到 resolve/reject,这就是我使用异步循环函数的原因。一切正常,问题在下单后上传,一切都按照预期的方式发送到服务器。问题是,我想从异步函数中获得 next_question()
的承诺,以便在将来处理它们的错误,并在使用 $q.all()
解决所有问题后做一些其他事情,但是问题是,据我所知 await
应该 return 一个承诺,但它只是 returns undefined
并且即使在循环完成后仍保持 undefined
所有的承诺,据说,解决。
我知道函数 next_question()
工作正常,因为如果直接调用它(在异步函数之外,直接从 set_survey_questions()
调用)它会保存问题并且 returns 只是承诺正如它应该的那样。
我对 angular 甚至 javascript 都不是很有经验,所以欢迎您提供任何帮助或改进。
As far as I know the await should return a promise but it just returns undefined
没有。您应该 将 一个承诺传递给 await
运算符,然后它将阻止异步函数的执行,直到承诺得到解决并且 return 承诺的结果值。
The problem is that I would like to get the promises of next_question() from the async function to deal with their errors in the future
这似乎不符合您对顺序保存的要求。没有多重承诺,一次只有一个活动。如果它们中的任何一个出错,await
将抛出异常并且您的循环将停止。
我认为你真的应该简化为
async function set_survey_questions(response, this_survey, response_survey) {
let sur_questions = QuestionsService.get({
actsern: this_survey.actsern
});
await sur_questions.$promise;
try {
for (const quest in response_survey) {
await next_question(response, this_survey, response_survey, sur_questions, quest);
}
console.log("Uploaded successfully all questions");
} catch (reason) {
console.log(reason);
}
}