Angular异步函数返回undefined而不是promise

时间:2018-03-26 19:41:05

标签: javascript angularjs asynchronous promise angular-promise

我正在使用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应该返回一个promise,但它只返回undefined并保持为undefined,即使在循环结束后,所有的承诺都应该解决。< / p>

我知道函数next_question()工作正常,因为如果直接调用它(在异步函数之外,直接来自set_survey_questions())它会保存问题并返回承诺,正如它所假设的那样。

我对角度甚至javascript都没有太多经验,所以欢迎您提出任何帮助或改进。

1 个答案:

答案 0 :(得分:1)

  

据我所知,await应该返回一个promise,但它只返回undefined

没有。您应该向<{1}}运算符传递一个承诺,然后它将阻止执行异步函数,直到承诺结算并返回承诺的结果值

  

问题是我希望从async函数中获取next_question()的承诺,以便将来处理它们的错误

这似乎与您的顺序保存要求不兼容。没有多重承诺,一次只有一个有效。如果其中任何一个错误,await将抛出异常,您的循环将停止。

我认为你真的应该简化到

await