小编aka*_*odi的帖子

在 promise 中定义异步函数

我正在使用.thenPromise编写代码。现在,我决定使用 await/async 来编写它。我已经调用的函数add_Lessons内的承诺,然后调用另一个函数。那么该功能。这是我使用.then 的代码。

function create_section(sections,course_ID,i) {
return new Promise(
    (resolve) => {
        var s_duration = 0;
        var sname = sections[i].name;
        var s_obj = {
            //some object;
        }
        var section_id;
        DB.section.create(s_obj,function (err, data_s) 
        {
            if (err) return next(err);
            section_id = data_s._id;
            var lesson = sections[i].lessons;
            add_lessons(lesson,section_id,i)
            .then(function(arr){
                resolve(arr);
            })
        });
    }
);
};
Run Code Online (Sandbox Code Playgroud)

这是使用await/async的代码。

function create_section(sections,course_ID,i) {
return new Promise(
    async function resolve() {
        var s_duration …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb node.js promise

4
推荐指数
1
解决办法
8070
查看次数

标签 统计

javascript ×1

mongodb ×1

node.js ×1

promise ×1