【发布时间】:2017-12-15 02:30:51
【问题描述】:
我有一个功能在节点环境中运行良好。该函数使用 Promise、S3 调用和 then 和 catch 调用回调,其中包含相关的 200/500 statusCode 和消息正文。
现在我将其部署为 lambda 函数,并在其周围有一个包装器,如下所示:
module.exports.getAvailableDates = (event, context, callback) => {
const lambdaParams = retrieveParametersFromEvent(event);
console.log(`Got the criteria`);
module.exports.getFilteredDates(lambdaParams.startDate,
lambdaParams.endDate, callback);
console.log(`Returning without the internal function results`);
};
内部函数如下所示:
module.exports.function getFilteredDates(startDate, endDate) {
const dateSet = new Set();
return new Promise((resolve, reject) => {
const getAllDates = (isDone) => {
if (isDone) {
const Dates = Array.from(dateSet).join();
resolve(Dates);
return;
}
getTestedDates(startDate, endDate, region, func, memory,
lastDateKey, dateSet).then(getAllDates).catch((error) => {
reject(error);
});
};
lastDateKey = '';
getTestedDates(startDate, endDate, region, func, memory,
lastDateKey, dateSet).then(getAllDates).catch((error) => {
reject(error);
});
});
}
更内部的函数看起来很相似,只是它实际查询 S3 数据库并从中返回与日期条件匹配的键列表。
在 AWS CloudWatch 日志中,我看到了两个打印件,并且仅在它们之后是内部函数输出。我的理解是 lambda 函数并没有等待带有承诺的内部函数实际完成它的工作(包括内部等待承诺)并且返回给我一个错误的状态。我能做什么?
【问题讨论】:
-
你能分享你的
promise代码
标签: javascript amazon-web-services aws-lambda es6-promise