提问人:best_of_man 提问时间:1/10/2023 最后编辑:best_of_man 更新时间:1/11/2023 访问量:141
Promise 和 Callback,哪个更适合与 NodeJS 一起使用?
Promise or Callback, which one is better to use with NodeJS?
问:
我发现有 2 种不同的方法可以使用 or 编写节点函数,第一种方法就像下面定义函数一样:promise
callback
findByEmail
class Users{
static async findByEmail(email: any ) : Promise<Users | undefined>{
const user: any = await Pools.execute(
"SELECT * FROM users WHERE email = ?",
[email])
.then(rows => {
return rows[0];
})
.catch(err => console.log(err) );
return user;
};
}
router.post(
"/api/users/signin",
async (req: Request, res: Response , next: NextFunction) => {
const { email, password } = req.body;
const existingUser = await Users.findByEmail(email);
});
第二种方式是这样的:
declare global {
namespace Express {
interface Response {
user?: Users;
}
}
}
static async findByEmail(req: Request, res: Response) {
const user = await Pools.execute(
"SELECT * FROM users WHERE email = ?",
[req.body.email])
.then(rows => {
res.user = rows[0];
})
.catch(err => console.log(err) );
};
router.post(
"/api/users/signin",
async (req: Request, res: Response , next: NextFunction) => {
await Users.findByEmail(req, res);
const existingUser = res.user;
});
我不确定这是否是一个“基于意见”的问题?然而,我问这个问题的目的是想知道哪种方式是更好的做法,为什么?根据性能和其他可能的问题?
特别是我想知道最好使用返回值编写函数或使用响应
对象将返回
值添加到 then(
) 函数中的函数中,例如 .then(res.user = user) 而不是 const user = await pool.execute(SELECT ...)
?
答:
1赞
jfriend00
1/11/2023
#1
这是一种刺穿的方法,可以进行以下改进:
- 变成一个独立于 和 对象的实用函数,因此可以普遍使用。
findByEmail()
req
res
- 正确地将所有错误从调用方传播回调用方。
findByEmail()
- 对传入的电子邮件字段实施一些验证检查,并为此创建单独的错误路径。
- 在服务器上记录所有错误
- 检查数据库请求中的所有错误条件
- 不混合和 .
.then()
await
代码如下:
// resolves to null if email not found
// rejects if there's a database error
static async findByEmail(email) {
const rows = await Pools.execute("SELECT * FROM users WHERE email = ?", [email]);
if (!rows || !rows.length || !rows[0]) {
return null;
}
return rows[0];
};
router.post("/api/users/signin", async (req: Request, res: Response, next: NextFunction) => {
try {
// validate incoming parameters
if (!req.body.email) {
let errMsg = "No email value present in incoming signin request";
console.log(errMsg);
res.status(400).send(errMsg);
return;
}
let user = await Users.findByEmail(req.body.email);
if (!user) {
// do whatever you would do if user tries to signin with non-existent email
// presumably return something like a 404 status
} else {
// do whatever you wanted to do here with the user object after login
}
} catch(e) {
// some sort of server error here, probably a database error, not the client's fault
console.log(e);
res.sendStatus(500);
}
});
评论
0赞
best_of_man
1/11/2023
非常感谢您的帮助。但似乎您删除了所有功能。我认为使用是在 NodeJS 中编写异步函数的现代首选方式,并尝试尽可能多地使用函数。我对使用 .我不知道我应该在哪里使用它们?then()
then()
then()
then()
1赞
jfriend00
1/11/2023
@best_of_man - 如果您正在使用 ,请不要使用 或 。 是 的替换,周围是 的替换(并且还会捕获其他错误)。await
.then()
.catch()
await
.then()
try/catch
await
.catch()
0赞
best_of_man
1/11/2023
您的解决方案有效,但我仍然想知道当您尚未定义函数的返回值时它是如何工作的?我的意思是像这样findByEmail()
static async findByEmail(email) : Promise<User>{
0赞
jfriend00
1/11/2023
@best_of_man - 我不是 TypeScript 的人,所以你必须填写 TypeScript 返回值部分。
评论
await
await
.then()
findByEmail()
.catch(err => console.log(err))
undefined
throw err