使用嵌套的async.forEachSeries和async.waterfall来控制Lambda Node.js代码的排序

时间:2016-02-23 15:39:43

标签: node.js asynchronous callback aws-lambda alexa-skills-kit

我有一部分在Lambda中运行的Alexa技能,可以向玩家发送文本。我使用async.waterfall来控制调用dynamodb表的顺序,以获得该播放器的正确ARN(将文本发送到的位置),然后通过AWS SNS发布SMS。如果playerToReceive []中只有1个玩家,则可行。但是我需要它为多个玩家工作。为此,我在async.forEachSeries中嵌套了async.waterfall(也试过forEach),但我的回调结构错了。我认为我需要应用this answer的逻辑,但我对此很多并且很挣扎。

        async.forEachSeries(    playersToReceive, // array of items             
            function(receivingPlayer, callback){                
                async.waterfall([                   
                    function (callback) {
                        session.attributes.phoneKey = receivingPlayer;
                        callback(null);
                    },                      
                    function (callback) {                   
                        playerStorage.loadPlayer(session, function (newLoadedPlayer) {
                            if (newLoadedPlayer == 'playerNotFound' || newLoadedPlayer == 'errorLoadingPlayer') {
                                problems = true; // set problems flag for later
                                callback(null);
                            } else {
                                var ARNtoSend = newLoadedPlayer.data.TopicARN.S;
                                callback(null, ARNtoSend);
                            };
                        })
                    },                              
                    function (ARNtoSend, callback) {
                        playerSMS.publishSMS(ARNtoSend, textToSend, function (success) {
                            if (success == false) {problems = true}; // set problems flag for later
                            callback(null);
                        })                                                      
                    }                                                       
                ], function (err, result) {
                    if (err) console.log(err, "SMS text had a problem sending.");
                    if (!err) console.log(null, "SMS text was successfully sent.");
                });                                                                                 
                callback();
            },          
            function(err){
                // All tasks are now complete               
                speechText = 'OK, text sent.';
                if (problems == true) {
                    speechText += ' . But there was a problem sending it to some players.'
                }
                response.tell(speechText);                                                                                                                      
            }
        );

1 个答案:

答案 0 :(得分:1)

我想通了 - 外部回调需要进入瀑布的关闭功能。通过这种改变使其有效:

            async.forEachSeries(    playersToReceive, // array of items 

            function(receivingPlayer, callback){

                async.waterfall([

                    function (callback) {
                        session.attributes.phoneKey = receivingPlayer;
                        callback(null);
                    },

                    function (callback) {                   
                        playerStorage.loadPlayer(session, function (newLoadedPlayer) {
                            if (newLoadedPlayer == 'playerNotFound' || newLoadedPlayer == 'errorLoadingPlayer') {
                                problems = true; // set problems flag for later
                                callback(null);
                            } else {
                                var ARNtoSend = newLoadedPlayer.data.TopicARN.S;
                                callback(null, ARNtoSend);
                            };
                        })
                    },

                    function (ARNtoSend, callback) {
                        playerSMS.publishSMS(ARNtoSend, textToSend, function (success) {
                            if (success == false) {problems = true}; // set problems flag for later
                            callback(null);
                        })                                                      
                    }       


                ], function (err, result) {
                    if (err) console.log(err, "SMS text had a problem sending.");
                    if (!err) console.log(null, "SMS text was successfully sent.");
                    callback();
                });                     

            },          

            function(err){
                // All tasks are now complete               
                speechText = 'OK, text sent.';
                if (problems == true) {
                    speechText += ' . But there was a problem sending it to some players.'
                }
                response.tell(speechText);                                                                                                                      
            }
        );