节点回调地狱+异步通知

时间:2015-05-01 15:48:49

标签: node.js callback async.js asynccallback

我是节点的新手,并且最近一直在阅读一篇关于如何最好地避免回调地狱的文章。感谢我的新手能力,我在我的代码中积累了一些金字塔,所以我点击谷歌看看如何避免它。

我正在使用Salesforce Marketing Cloud API构建应用程序...

这是我的情景。

  1. 检查我帐户中的特定文件夹是否存在。
  2. 如果存在则返回文件夹的ID
  3. 如果不存在,请搜索父文件夹并获取其ID
  4. 创建文件夹并使用ParentFolders ID作为属性。
  5. 所以在旧学校的代码中,这是非常简单的,只需按顺序执行步骤1-4。

    在节点I中,我得到了一个带回调函数的函数,该函数带有回调函数。

    感谢Google,我遇到了异步库,但我不太清楚如何最好地利用它。

    这是我的一段代码,它是我从运行服务器的main.js文件调用的更大模块的一部分

    MyHelper.prototype.test = function(name, req, callback) {
    
        async.series([
            function(next){
                etHelper.folder_find("dataextension", name, next);  
            },function(next){
                etHelper.folder_find("dataextension", "Data Extensions", next); 
            }
        ],
        function(err, results){
            console.log(results)
        });
    
    
    };
    

    我应该使用Async的瀑布方法吗?如果是这样,我将如何检查步骤1中的ID,如果它不是null退出

    不确定它是否有帮助,但这是我从我的ETHelper方法构建的响应

    [ { Success: false,
    Status: 'Warning',
    Message: 'Folder Not Found',
    Type: 'dataextension',
    Name: '10513542_SQLHelper_Test',
    ID: null },
      { Success: true,
    Status: 'OK',
    Message: 'Folder Found',
    Type: 'dataextension',
    Name: 'Data Extensions',
    ID: '1040721' } ]
    

    我真的想在这里学习,但我对我遇到的信息感到有些不知所措。似乎有100种不同的方式来做事情,而且很难知道选择哪一种。

    最后,我不想破坏这篇文章,但这是我提出来处理这个问题的原始方法,这是我知道的一团糟。

    SQLHelper.prototype.checkInstall = function(name, req, callback) {
    
        var response = new Object();
        var deDone =  false;
        var qDone = false;
        /*
         *********************************************************************************************************
         * 
         * This function will look for the SQLHelper folders. If they are not found they will be added
         *
         *********************************************************************************************************
        */
    
        //Setup the DEFolder Lookup Filter
        var deFolderParms = {
            objectType: "DataFolder",
            props: ["Name", "ID", "ContentType"],
            filter: {
                leftOperand: {
                    leftOperand: 'Name',
                    operator: 'equals',
                    rightOperand: name
                },
                operator: 'AND',
                rightOperand: 
                {
                    leftOperand: 'ContentType',
                    operator: 'equals',
                    rightOperand: "dataextension"
                }
            }
        };
    
        //Look for the Folder
        etHelper.getByName(deFolderParms, SoapClient, function(err, deFolderResponse){
            //console.log(deFolderResponse);
            if(deFolderResponse.length <=0)
            {
                //Folder Does not exist so create it
                //But first we need the Parent Folder
    
    
                var deParentFolderParms = {
                    objectType: "DataFolder",
                    props: ["Name", "ID", "ContentType"],
                    filter: {
                        leftOperand: {
                            leftOperand: 'Name',
                            operator: 'equals',
                            rightOperand: "Data Extensions"
                        },
                        operator: 'AND',
                        rightOperand: 
                        {
                            leftOperand: 'ContentType',
                            operator: 'equals',
                            rightOperand: "dataextension"
                        }
                    }
                };
                etHelper.getByName(deParentFolderParms, SoapClient, function(err, deParentFolderResponse){
                    //console.log(deParentFolderResponse);
                    if(deParentFolderResponse.length > 0){
                        var newFolderParms = {
                            objectType: "DataFolder", 
                            props: {
                                Name: name,
                                CustomerKey: name,
                                Description: "This folder is only to be used by the SQL Helper Application", 
                                ContentType: "dataextension", 
                                IsActive: true, 
                                IsEditable: true,
                                AllowChildren: false,
                                ParentFolder:{
                                    ID: deParentFolderResponse[0].ID    
                                },
                                Client:{
                                    ID: req.session.fuel.mid
                                }
                            },
                            options: {}
                        };
    
                        etHelper.create(newFolderParms, SoapClient, function(err, newFolderResponse){
                            //console.log(newFolderResponse);
                            if(newFolderResponse.length > 0){
                                deDone = true;
                                response.DEFolderID = newFolderResponse[0].NewID;
                                response.Status = true;
                                if(qDone)
                                {
                                    callback(response);
                                }
                            }
                            else{
                                response.Status = false;
                                callback(response);
                            }
    
                        });
                    }
                    else
                    {
                        response.Status = false;
                        callback(response);
                    }
                });
            }   
            else
            {
                deDone = true;
                response.DEFolderID = deFolderResponse[0].ID;
                response.Status = true;
                if(qDone)
                {
                    callback(response);
                }
            }
        });
    
    
        //Setup the DEFolder Lookup Filter
        var qFolderParms = {
            objectType: "DataFolder",
            props: ["Name", "ID", "ContentType"],
            filter: {
                leftOperand: {
                    leftOperand: 'Name',
                    operator: 'equals',
                    rightOperand: name
                },
                operator: 'AND',
                rightOperand: 
                {
                    leftOperand: 'ContentType',
                    operator: 'equals',
                    rightOperand: "queryactivity"
                }
            }
        };
    
        //Look for the Folder
        etHelper.getByName(qFolderParms, SoapClient, function(err, qFolderResponse){
            //console.log(qFolderResponse);
            if(qFolderResponse.length <=0)
            {
                //Folder Does not exist so create it
                //But first we need the Parent Folder
    
    
                var qParentFolderParms = {
                    objectType: "DataFolder",
                    props: ["Name", "ID", "ContentType"],
                    filter: {
                        leftOperand: {
                            leftOperand: 'Name',
                            operator: 'equals',
                            rightOperand: "Query"
                        },
                        operator: 'AND',
                        rightOperand: 
                        {
                            leftOperand: 'ContentType',
                            operator: 'equals',
                            rightOperand: "queryactivity"
                        }
                    }
                };
                etHelper.getByName(qParentFolderParms, SoapClient, function(err, qParentFolderResponse){
                    //console.log(qParentFolderResponse);
                    if(qParentFolderResponse.length > 0){
                        var qnewFolderParms = {
                            objectType: "DataFolder", 
                            props: {
                                Name: name,
                                CustomerKey: name,
                                Description: "This folder is only to be used by the SQL Helper Application", 
                                ContentType: "queryactivity", 
                                IsActive: true, 
                                IsEditable: true,
                                AllowChildren: false,
                                ParentFolder:{
                                    ID: qParentFolderResponse[0].ID 
                                },
                                Client:{
                                    ID: req.session.fuel.mid
                                }
                            },
                            options: {}
                        };
    
                        etHelper.create(qnewFolderParms, SoapClient, function(err, qnewFolderResponse){
                            //console.log(qnewFolderResponse);
                            if(qnewFolderResponse.length > 0){
                                qDone = true;
                                response.QueryFolderID = qnewFolderResponse[0].NewID;
                                response.Status = true;
                                if(deDone)
                                {
                                    callback(response);
                                }
                            }
                            else{
                                response.Status = false;
                                callback(response);
                            }
    
                        });
                    }
                    else
                    {
                        response.Status = false;
                        callback(response);
                    }
                });
            }   
            else
            {
                qDone = true;
                response.QueryFolderID = qFolderResponse[0].ID;
                response.Status = true;
                if(deDone)
                {
                    callback(response);
                }
            }
        });
    };
    

0 个答案:

没有答案