Promise.all - 运行 时间不对?
Promise.all - Running at wrong time?
我似乎无法让 Promise.all 发挥我认为应有的作用。
正如您从日志中看到的那样,"Streak" 和 "Last Activity" 等数据的承诺在接近尾声时已成功填充。但是我希望Promise.all仅在所有内容都已填充时才获取数据,目前它在所有内容未定义时在开头调用一次。
第一次使用 promises,非常感谢任何建议!
function buildResponse (UserId, weekNumber, yesterday, today, lastLogin) {
let currentStreak;
let getLastActivity;
let weekExercise;
let dayTotal;
let yesterdayTotal;
getUserData(UserId);
getUserData(UserId).then((data) => {
if (!data.Item || !data.Item.UserId) {
console.log("Get User Data Not Found");
} else {
currentStreak = data.Item.streak;
getLastActivity = data.Item.lastActivity;
console.log("Streak is: " + currentStreak);
console.log("Last activity is: " + getLastActivity);
}
});
getUserData(UserId).catch((err) => {
console.log("Get week data error " + err);
});
getWeekData(UserId, weekNumber);
getWeekData(UserId, weekNumber).then((data) => {
if (!data.Item || !data.Item.UserId) {
console.log("Week Data Not Found");
} else {
var yesterdayDay = "Monday";
weekExercise = data.Item.weekTotal;
dayTotal = data.Item.Monday;
yesterdayTotal = data.Item[yesterdayDay];
console.log("Days total is: " + dayTotal)
}
});
getWeekData(UserId, weekNumber).catch((err) => {
console.log("Get week data error " + err);
});
Promise.all([getWeekData(UserId, weekNumber), getUserData(UserId)]).then((data) => {
console.log("Promise all: " + dayTotal + " and " + currentStreak + " and " + getLastActivity);
});
}
获取用户数据:
function getUserData(UserId) {
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: "XXXXXXXX",
Key: {
"UserId": UserId,
}
};
return docClient.get(params).promise();
}
日志是:
2018-04-09T15:18:35.348Z 43df7459-3c09-11e8-b44c-1726d06a9efd Promise all: undefined and undefined and undefined
2018-04-09T15:18:35.366Z 43df7459-3c09-11e8-b44c-1726d06a9efd Update last
login succeeded: {
"Attributes": {
"lastLogin": "2018-04-09"
}
}
2018-04-09T15:18:35.387Z 43df7459-3c09-11e8-b44c-1726d06a9efd Streak is: 0
2018-04-09T15:18:35.405Z 43df7459-3c09-11e8-b44c-1726d06a9efd Last activity is: run
2018-04-09T15:18:35.406Z 43df7459-3c09-11e8-b44c-1726d06a9efd Reset Streak succeeded: {
"Attributes": {
"streak": 0
}
}
2018-04-09T15:18:35.426Z 43df7459-3c09-11e8-b44c-1726d06a9efd Days total is: 768
编辑 [09/04/2018]
运行 又一次,这次有两个承诺得到满足,但一个没有。关于添加什么以确保 Promise.all 等到所有内容都填满有什么建议吗?
2018-04-09T15:41:19.427Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Streak is: 0
2018-04-09T15:41:19.445Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Last activity is: run
2018-04-09T15:41:19.448Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Promise all: undefined and 0 and run
2018-04-09T15:41:19.486Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Update last login succeeded: {
"Attributes": {
"lastLogin": "2018-04-09"
}
}
2018-04-09T15:41:19.486Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Days total is: 128
2018-04-09T15:41:19.506Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Reset Streak succeeded: {
"Attributes": {
"streak": 0
}
}
END RequestId: 710113a9-3c0c-11e8-a906-cb494b7c4b47
REPORT RequestId: 710113a9-3c0c-11e8-a906-cb494b7c4b47 Duration: 2008.58 ms Billed Duration: 2100 ms Memory Size: 128 MB Max Memory Used: 38 MB
不要违背承诺的好处!在多个地方你做这样的事情:
getUserData(UserId).then((data) => {
// This line is executed *somewhen* when the data is found
// The below line is the real problem:
currentStreak = data.Item.streak;
});
那是错误的!您不应将数据传递到 promises then
处理程序之外。而是将依赖于它的代码 放入 then
处理程序,以便它能够在那里访问它,并且所有异步代码问题都消失了:
Promise.all(getUserData(UserId), getWeekData(UserId, weekNumber)).then(([userData, weekData]) => {
const currentStreak = userData.Item.streak;
const getLastActivity = userData.Item.lastActivity;
const yesterdayDay = "Monday";
const weekExercise = weekData.Item.weekTotal;
const dayTotal = weekData.Item.Monday;
const yesterdayTotal = weekData.Item[yesterdayDay];
/*...*/
}).catch(/* a proper error handler */);
我似乎无法让 Promise.all 发挥我认为应有的作用。
正如您从日志中看到的那样,"Streak" 和 "Last Activity" 等数据的承诺在接近尾声时已成功填充。但是我希望Promise.all仅在所有内容都已填充时才获取数据,目前它在所有内容未定义时在开头调用一次。
第一次使用 promises,非常感谢任何建议!
function buildResponse (UserId, weekNumber, yesterday, today, lastLogin) {
let currentStreak;
let getLastActivity;
let weekExercise;
let dayTotal;
let yesterdayTotal;
getUserData(UserId);
getUserData(UserId).then((data) => {
if (!data.Item || !data.Item.UserId) {
console.log("Get User Data Not Found");
} else {
currentStreak = data.Item.streak;
getLastActivity = data.Item.lastActivity;
console.log("Streak is: " + currentStreak);
console.log("Last activity is: " + getLastActivity);
}
});
getUserData(UserId).catch((err) => {
console.log("Get week data error " + err);
});
getWeekData(UserId, weekNumber);
getWeekData(UserId, weekNumber).then((data) => {
if (!data.Item || !data.Item.UserId) {
console.log("Week Data Not Found");
} else {
var yesterdayDay = "Monday";
weekExercise = data.Item.weekTotal;
dayTotal = data.Item.Monday;
yesterdayTotal = data.Item[yesterdayDay];
console.log("Days total is: " + dayTotal)
}
});
getWeekData(UserId, weekNumber).catch((err) => {
console.log("Get week data error " + err);
});
Promise.all([getWeekData(UserId, weekNumber), getUserData(UserId)]).then((data) => {
console.log("Promise all: " + dayTotal + " and " + currentStreak + " and " + getLastActivity);
});
}
获取用户数据:
function getUserData(UserId) {
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: "XXXXXXXX",
Key: {
"UserId": UserId,
}
};
return docClient.get(params).promise();
}
日志是:
2018-04-09T15:18:35.348Z 43df7459-3c09-11e8-b44c-1726d06a9efd Promise all: undefined and undefined and undefined
2018-04-09T15:18:35.366Z 43df7459-3c09-11e8-b44c-1726d06a9efd Update last
login succeeded: {
"Attributes": {
"lastLogin": "2018-04-09"
}
}
2018-04-09T15:18:35.387Z 43df7459-3c09-11e8-b44c-1726d06a9efd Streak is: 0
2018-04-09T15:18:35.405Z 43df7459-3c09-11e8-b44c-1726d06a9efd Last activity is: run
2018-04-09T15:18:35.406Z 43df7459-3c09-11e8-b44c-1726d06a9efd Reset Streak succeeded: {
"Attributes": {
"streak": 0
}
}
2018-04-09T15:18:35.426Z 43df7459-3c09-11e8-b44c-1726d06a9efd Days total is: 768
编辑 [09/04/2018]
运行 又一次,这次有两个承诺得到满足,但一个没有。关于添加什么以确保 Promise.all 等到所有内容都填满有什么建议吗?
2018-04-09T15:41:19.427Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Streak is: 0
2018-04-09T15:41:19.445Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Last activity is: run
2018-04-09T15:41:19.448Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Promise all: undefined and 0 and run
2018-04-09T15:41:19.486Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Update last login succeeded: {
"Attributes": {
"lastLogin": "2018-04-09"
}
}
2018-04-09T15:41:19.486Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Days total is: 128
2018-04-09T15:41:19.506Z 710113a9-3c0c-11e8-a906-cb494b7c4b47 Reset Streak succeeded: {
"Attributes": {
"streak": 0
}
}
END RequestId: 710113a9-3c0c-11e8-a906-cb494b7c4b47
REPORT RequestId: 710113a9-3c0c-11e8-a906-cb494b7c4b47 Duration: 2008.58 ms Billed Duration: 2100 ms Memory Size: 128 MB Max Memory Used: 38 MB
不要违背承诺的好处!在多个地方你做这样的事情:
getUserData(UserId).then((data) => {
// This line is executed *somewhen* when the data is found
// The below line is the real problem:
currentStreak = data.Item.streak;
});
那是错误的!您不应将数据传递到 promises then
处理程序之外。而是将依赖于它的代码 放入 then
处理程序,以便它能够在那里访问它,并且所有异步代码问题都消失了:
Promise.all(getUserData(UserId), getWeekData(UserId, weekNumber)).then(([userData, weekData]) => {
const currentStreak = userData.Item.streak;
const getLastActivity = userData.Item.lastActivity;
const yesterdayDay = "Monday";
const weekExercise = weekData.Item.weekTotal;
const dayTotal = weekData.Item.Monday;
const yesterdayTotal = weekData.Item[yesterdayDay];
/*...*/
}).catch(/* a proper error handler */);