使用嵌套的 async.forEachSeries 和 async.waterfall 来控制 Lambda Node.js 代码的排序
using nested async.forEachSeries and async.waterfall to control sequencing of Lambda Node.js code
我在 Lambda 中有一部分 Alexa 技能 运行 可以向玩家发送文本。我正在使用 async.waterfall 来控制调用 dynamodb table 的顺序,以便为该播放器获取正确的 ARN(将文本发送到何处),然后通过 AWS 发布 SMS社交网络。如果 playersToReceive[] 中只有 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);
}
);
我明白了 - 外部回调需要进入瀑布的关闭函数。通过该更改使其有效:
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);
}
);
我在 Lambda 中有一部分 Alexa 技能 运行 可以向玩家发送文本。我正在使用 async.waterfall 来控制调用 dynamodb table 的顺序,以便为该播放器获取正确的 ARN(将文本发送到何处),然后通过 AWS 发布 SMS社交网络。如果 playersToReceive[] 中只有 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);
}
);
我明白了 - 外部回调需要进入瀑布的关闭函数。通过该更改使其有效:
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);
}
);