2

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

        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);                                                                                                                      
            }
        );
4

1 回答 1

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);                                                                                                                      
            }
        );  
于 2016-02-23T22:18:24.890 回答