Javascript 如何修复-等待而不是等待
当我运行异步函数时,我希望使用一个wait声明,它不在“then”中,而是类似于:Javascript 如何修复-等待而不是等待,javascript,mongodb,promise,async-await,Javascript,Mongodb,Promise,Async Await,当我运行异步函数时,我希望使用一个wait声明,它不在“then”中,而是类似于: const todayTotalVisitor = await getLastDayVisitors(); 这样,等待就不是等待 async function sumMonth() { const today = new Date(); if (today.getDate() == 1) return 0; else { const todayTotalVisitor = await getLastD
const todayTotalVisitor = await getLastDayVisitors();
这样,等待就不是等待
async function sumMonth() {
const today = new Date();
if (today.getDate() == 1) return 0;
else {
const todayTotalVisitor = await getLastDayVisitors();
//query - last yestardy
Counter.find({date: {$gte: beforeYesterday, $lt:yesterday
}}).then((result1) => {
//get the totalVisitors of yestardy
const monthlyYestardy = result1[0].monthly;
//get today total visitor
return todayTotalVisitor + monthlyYestardy;
}).catch((err) => {
console.log(err);
});
}}
在这种情况下,TotalVisitor是未定义的
GetLastDay访客:
async function getLastDayVisitors() {
//query - last yestardy
Counter.find({date: {$gte: beforeYesterday, $lt:yesterday
}}).then((result1) => {
//get the totalVisitors of yestardy
const TotalVisitorYesterday = result1[0].totalVisitors;
//query - general
Counter.find({name: 'general' }).then((result2) => {
//get the totalVisitors overall
const TotalVisitorOverAll = result2[0].totalVisitors;
//return the delta
return ( TotalVisitorOverAll-TotalVisitorYesterday);
}).catch((err) => {
console.log(err);
});
}).catch((err) => {
console.log(err);
});
}
谢谢。您的getLastDayVisitors没有返回任何内容,也没有等待任何内容,因此承诺会立即解析为
未定义的
,而无需等待任何异步完成
将getLastDayVisitors更改为使用Wait,因为它已经是异步的
其他更改纯粹是在异步中使用await,而不是await and。然后-使用其中一个
async function getLastDayVisitors() {
const result1 = await Counter.find({date: {$gte: beforeYesterday, $lt:yesterday }});
//get the totalVisitors of yestardy
const TotalVisitorYesterday = result1[0].totalVisitors;
//query - general
const result2 = await Counter.find({name: 'general' })
//get the totalVisitors overall
const TotalVisitorOverAll = result2[0].totalVisitors;
//return the delta
return ( TotalVisitorOverAll-TotalVisitorYesterday);
}
同时重写calleth
,因为它也是async
async function sumMonth() {
const today = new Date();
if (today.getDate() == 1) return 0;
const todayTotalVisitor = await getLastDayVisitors();
//query - last yestardy
const result1 = await Counter.find({date: {$gte: beforeYesterday, $lt:yesterday }})
//get the totalVisitors of yestardy
const monthlyYestardy = result1[0].monthly;
//get today total visitor
return todayTotalVisitor + monthlyYestardy;
}
注意,我已经删除了错误处理,因为在您使用它的地方,可能会导致比修复更多的问题
使用类召唤
sumMonth()
.then(result => doSomethingWitf(result))
.catch(err => handleTheError(err));
或者在异步函数中使用它
try {
result = await sumMonth();
// do something with it
} catch(err) {
// handle err here
}
您的getLastDayVisitors没有返回任何内容,也没有等待任何内容,因此承诺会立即解析为
未定义
,而无需等待任何异步完成
将getLastDayVisitors更改为使用Wait,因为它已经是异步的
其他更改纯粹是在异步中使用await,而不是await and。然后-使用其中一个
async function getLastDayVisitors() {
const result1 = await Counter.find({date: {$gte: beforeYesterday, $lt:yesterday }});
//get the totalVisitors of yestardy
const TotalVisitorYesterday = result1[0].totalVisitors;
//query - general
const result2 = await Counter.find({name: 'general' })
//get the totalVisitors overall
const TotalVisitorOverAll = result2[0].totalVisitors;
//return the delta
return ( TotalVisitorOverAll-TotalVisitorYesterday);
}
同时重写calleth
,因为它也是async
async function sumMonth() {
const today = new Date();
if (today.getDate() == 1) return 0;
const todayTotalVisitor = await getLastDayVisitors();
//query - last yestardy
const result1 = await Counter.find({date: {$gte: beforeYesterday, $lt:yesterday }})
//get the totalVisitors of yestardy
const monthlyYestardy = result1[0].monthly;
//get today total visitor
return todayTotalVisitor + monthlyYestardy;
}
注意,我已经删除了错误处理,因为在您使用它的地方,可能会导致比修复更多的问题
使用类召唤
sumMonth()
.then(result => doSomethingWitf(result))
.catch(err => handleTheError(err));
或者在异步函数中使用它
try {
result = await sumMonth();
// do something with it
} catch(err) {
// handle err here
}
getLastDayVisitors
是否正确返回解析为所需值的承诺。。。换句话说,Dan在下面写的是:getLastDayVisitors
的代码可以显示吗?另外,return Counter.find({date…
因为你的方法不会在任何地方被调用都等待承诺。到底是什么让你认为todayTotalVisitor是未定义的?你在哪里记录它?我认为这里更大的问题是你的函数根本不返回任何东西,所以它的结果总是未定义的。暗示在计数器之前添加return。find
在getLastDayVisitors
-实际上,它比这要复杂一点,getLastDayVisitors
返回一个正确解析为您所需值的承诺?…换句话说,Dan在下面写了什么:P您可以显示getLastDayVisitors
的代码吗?另外,返回计数器。find({date…
因为你的方法不会在任何地方被调用都等待承诺。到底是什么让你认为todayTotalVisitor是未定义的?你在哪里记录它?我认为这里更大的问题是你的函数根本不返回任何东西,所以它的结果总是未定义的。暗示在计数器之前添加return。find
在getLastDayVisitors
中-实际上它比这要复杂一点,很抱歉的位置不正确(
在“用法”示例中:p非常感谢,它是有效的。你能解释一下为什么它一开始不起作用(为什么函数体中与“then”的集成没有起作用?)Ori对的错误位置表示担忧(“用法”示例中的非常感谢,它是有效的。您能否解释一下为什么它一开始不起作用(为什么函数体中与“then”的集成没有起作用?)