如何在 jquery 中获得正确的嵌套函数

How to get the correct nested function in jquery

我有这个嵌套函数,但它只调用了前两个函数,然后跳过了其他函数。添加它太快重定向到另一个页面。我想要实现的是这个并确保每个实例在继续另一个实例之前完成其工作:

场景:
一审 // 首先调用
Sec 实例 // 接下来调用 then
第三个实例 // next called then
第四个实例 // 最后调用

请问我做错了什么吗?

这是我做的:

            if (db_name != "" && contact != "" && email !="") 
            {
                // createdb
                session.rpc('/custom/createdb',
                {
                    db_name: db_name,
                }).then(function() 
                {
                    console.log("Database created successfully")
                
                    //initdb
                    session.rpc('/custom/initdb', 
                    {
                        db_name: db_name,
                    }).then(function() 
                    {
                        console.log("Database initialized successfully")
                
                        // IT SKIPS HERE
                        //installapps
                        session.rpc('/custom/installapps', 
                        {
                            db_name: db_name,
                        }).then(function() 
                        {
                            console.log("Apps installed successfully")
                        
                            // AND HERE TOO
                            //createaccount
                            session.rpc('/custom/createaccount', 
                            {
                                db_name : db_name, 
                                contact_name: contact, 
                                email_from: email,
                            }).then(function () 
                            {
                                console.log("User account created successfully")
                            }); 
                        });
                    }); 
                })
              }

如果您编写的基于 promise 的代码不断嵌套越来越深,那么您就做错了什么。 Promise 的存在正是为了避免这种圣诞树类型的代码。

不是嵌套得越来越深,return 您的每个 RPC 调用给您的承诺。您还需要 return 来自辅助函数的顶级承诺,因此调用代码可以等到一切完成:

function creacteAccount(db_name, contact, email) {
    if (!(db_name > "" && contact > "" && email > "")) {
        return Promise.reject("One of the parameters is empty");
    }
    return session.rpc('/custom/createdb', {db_name: db_name})                  // return here
        .then(function () {
            console.log("Database created successfully");
            return session.rpc('/custom/initdb', {db_name: db_name});           // and here
        }).then(function () {
            console.log("Database initialized successfully");
            return session.rpc('/custom/installapps', {db_name: db_name});      // and here
        }).then(function () {
            console.log("Apps installed successfully");
            return session.rpc('/custom/createaccount', {                       // and here
                db_name : db_name, 
                contact_name: contact, 
                email_from: email
            });
        }).then(function () {
            console.log("User account created successfully");
        });
}

在调用代码的 .then() 中进行重定向,在 .catch() 中进行错误处理:

creacteAccount("myDB", "contact", "email").then(function () {
    // redirect...
}).catch(function (err) {
    alert("Could not create account! (" + err + ")");
});