javascript - Angular 异步函数返回未定义而不是 Promise

标签 javascript angularjs asynchronous promise angular-promise

我正在使用一个 Angular Controller ,该 Controller 会将上传到服务器的一个或多个调查中的不确定数量的问题添加到具有相同名称的现有调查中,这只是为了理解、上传、后台处理-结束并返回响应工作完美,这里真正的问题是上传新问题的具体部分,它具有以下功能:

//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() 的 promise 来处理将来的错误,并在使用 解决所有错误后做一些其他事情$q.all(),但问题是据我所知 await 应该返回一个 promise ,但它只返回 undefined 并保持为 未定义即使在循环完成并且所有 promise 都解决之后。

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

我对 Angular 甚至 javascript 都不是很有经验,所以欢迎您能想到的任何帮助或改进。

最佳答案

As far as I know the await should return a promise but it just returns undefined

没有。您应该向 await 运算符传递一个 Promise,然后它会阻止异步函数的执行,直到 Promise 得到解决并返回结果值的 promise 。

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

这似乎不符合您顺序保存的要求。没有多个 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);
    }
}

关于javascript - Angular 异步函数返回未定义而不是 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499368/

相关文章:

javascript - 如何将变量值插入for循环数组

JavaScript Promise 不执行 .then()

javascript - 将已排序的对象除以 4 以将它们列在 col-md-3 中

javascript - 如何在C#中声明相对路径

Javascript null 检查未按预期工作

asp.net - 异步 HttpApplication 事件会等到它们返回吗?

javascript - 如何从方法 JS 访问类变量

javascript - 文件上传后返回空对象 - Apollo graphql

javascript - Wordpress JS 引用 var 错误

javascript - Angular-Schema-Form 在 SCHEMA 更改时更改 FORM