如何在异步函数中重新组织我的收益? [英] How can I reorganize my return in an async function?
问题描述
findById(id){返回Model.findById(id)}
我需要重新组织用户数据
的返回,如下所示:
{名称:汤姆",年龄:57}
变成这样:
{消息:成功找到用户",成功:对,用户:user}
到目前为止,我可以通过 Promise"then"
部分来进行管理,如下所示:
返回Model.findById(id).then(user => {如果(!user){logger.warn(`找不到ID为$ {id}`的用户);返回{消息:找不到ID为$ {id}的用户,成功:false,用户:null};}返回{消息:`ID为$ {id}的用户,成功:true,用户:user};}).catch(err => {如果(错误){logger.error(err.message);返回{消息:err.message,成功:否,用户:null};}})
我可以对 async/await
进行同样的操作,并返回我的reorninazed返回吗?因为到目前为止,它从数据库返回了 user对象
:
async findById(id){返回等待this.model.findById(id,function(user,err){如果(错误){console.log('test');logger.error(err);返回{消息:err.message,成功:否,用户:null};}如果(!user){logger.warn(`找不到ID为$ {id}`的用户);返回{消息:找不到ID为$ {id}的用户,成功:false,用户:null};}返回{消息:`ID为$ {id}的用户,成功:true,用户:user};});}
提前谢谢!
大多数数据库API不同时支持回调和Promise.如果传递回调,则它们不会返回承诺.选择一种或另一种.您第一种使用 .then()
的方法可以很好地工作,因为这都是基于promise的.
您的第二种方法不起作用,因为您要传递常规回调.这告诉数据库不要返回承诺,因为您使用的是较旧的回调样式,但是您尝试使用该承诺.
如果您想使用 async/await
,则可以这样操作:
async findById(id){尝试 {让用户=等待this.model.findById(id);如果(用户){返回{消息:`ID为$ {id}的用户,成功:true,用户:user};} 别的 {logger.warn(`找不到ID为$ {id}`的用户);返回{消息:找不到ID为$ {id}的用户,成功:false,用户:null};}} catch(e){logger.error(err);返回{消息:err.message,成功:否,用户:null};}}
仅供参考,您可以从第一个代码块中的 .catch()
处理程序中删除 if(err)
测试.如果触发了 .catch()
,则出现错误-您无需测试是否存在该错误.
let's say I've got a function where I'm fetching for some data from a DB.
findById(id) {
return Model.findById(id)
}
I need to reorganize the return from the user data
like this:
{
name: "Tom",
age: 57
}
into something like this:
{
message: "User is found successfully",
success: true,
user: user
}
So far I can manage with that with a Promise "then"
section, like this:
return Model.findById(id)
.then(user => {
if (!user) {
logger.warn(`Coundn't find user with id: ${id}`);
return { message: `Coundn't find user with id: ${id}`, success: false, user: null };
}
return { message: `User with id: ${id}`, success: true, user: user };
})
.catch(err => {
if (err) {
logger.error(err.message);
return { message: err.message, success: false, user: null };
}
})
Can I do the same with a async/await
and return my reorninazed return?
Because so far it returns the user object
from the DB:
async findById(id) {
return await this.model.findById(id, function (user, err) {
if (err) {
console.log('test');
logger.error(err);
return { message: err.message, success: false, user: null };
}
if (!user) {
logger.warn(`Coundn't find user with id: ${id}`);
return { message: `Coundn't find user with id: ${id}`, success: false, user: null };
}
return { message: `User with id: ${id}`, success: true, user: user };
});
}
Thanks in advance!
Most database APIs do NOT support both a callback and a promise at the same time. If you pass a callback, they do not return a promise. Pick one style or the other. Your first approach using .then()
works just fine as that is all promise-based.
Your second approach does not work because you're passing a regular callback. That tells the database to NOT return a promise because you're using the older callback style, but you're trying to use that promise.
If you want to use async/await
, you could do so like this:
async findById(id) {
try {
let user = await this.model.findById(id);
if (user) {
return { message: `User with id: ${id}`, success: true, user: user };
} else {
logger.warn(`Coundn't find user with id: ${id}`);
return { message: `Coundn't find user with id: ${id}`, success: false, user: null };
}
} catch(e) {
logger.error(err);
return { message: err.message, success: false, user: null };
}
}
FYI, you can remove the if (err)
test in the .catch()
handler from your first code block. If .catch()
is triggered, there is an error - you don't need to test if one is there.
这篇关于如何在异步函数中重新组织我的收益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!