【问题标题】:Angular async function returns undefined instead of promise角度异步函数返回未定义而不是承诺
【发布时间】:2018-03-26 19:41:04
【问题描述】:

我正在使用 Angular 控制器,它将从上传到服务器的一个或多个调查中添加未确定数量的问题到具有相同名称的现有调查中,这只是为了理解、上传、处理 -结束和返回响应完美无缺,这里真正的问题是刚刚上传新问题的特定部分,它具有以下功能:

//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);
        });
    });
};

问题是我必须同步保存问题,在保存下一个之前等待一个解决/拒绝,这就是我使用异步循环功能的原因。一切正常,问题在订单后上传,一切都按照预期的方式发送到服务器。问题是我想从异步函数中获得next_question() 的承诺,以在将来处理它们的错误,并在使用$q.all() 解决它们之后做一些其他的事情,但问题在于据我所知,await 应该返回一个承诺,但它只返回undefined 并保持为undefined,即使在循环完成并且所有承诺都应该解决之后。

我知道函数 next_question() 可以正常工作,因为如果直接调用它(在异步函数之外,直接从 set_survey_questions() 调用),它会保存问题并按预期返回承诺。

我对 Angular 甚至 javascript 都不是很有经验,因此欢迎您提供任何帮助或改进。

【问题讨论】:

  • 尝试声明您的变量:const promises = []const promise = await ...
  • 不返回承诺数组。立即致电$q.all
  • 是的,没关系!
  • @Bergi 如果我理解正确,您指出的只是一个改进并感谢您的提示,但问题仍然是await 根本没有返回任何承诺,如果我放入一个数组并返回或只是在函数本身内部调用$q.all()

标签: javascript angularjs asynchronous promise angular-promise


【解决方案1】:

据我所知,等待应该返回一个承诺,但它只是返回未定义

没有。你应该传递一个promise给await操作符,然后它会阻塞异步函数的执行直到promise被解决并返回promise的result值

问题是我想从 async 函数中获得 next_question() 的 Promise 来处理它们将来的错误

这似乎与您对顺序保存的要求不兼容。没有多个承诺,一次只有一个活动。如果其中任何一个出错,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);
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-22
    相关资源
    最近更新 更多