如何解决处理类中的回调
我正在构建类以查找mongodb文档并快速对其进行操作。这是UserCursor类。 (不谈论MongoDB的光标)
exports { UserCursor };
class UserCursor {
private __id: object;
constructor(query: { _id?: object,otherId?: number }) {
let { _id,otherId } = query; // Shortens the vars' name
if (!_id && !otherId) return; // Checks if 1 identifier is provided
if (_id) { // If a _id is provided
Users.findOne({ _id },(err,user) => {
this.__id = user._id;
});
} else if (otherId) { // If a otherId is provided
Users.findOne({ otherId },user) => {
console.log(1); // Debug,you'll see later
this.__id = user._id;
});
}
}
// Returns this.__id (which should have been initialized in the constructor)
get _id() {
console.log(2)
return this.__id;
}
}
运行时,控制台返回
2
1
我认为您遇到了问题:_id
运行后,构造函数中的mongo回调继续运行。由于每次使用该类时都会激活构造函数,所以我该怎么办?
解决方法
我尚不清楚,您到底想发生什么以及如何使用此类,但我想您想实例化它,然后能够立即获取_id。如果不是这种情况,您仍然可以从我的答案中获得一些有用的信息。请随时提供更多详细信息,我将对其进行更新。
因此,如果您这样做,mongodb操作是异步的
const cursor = new UserCursor(...)
console.log(cursor._id)
(我假设您要这样做),首先将运行该线程中的所有操作,包括对get _id()
的调用,然后将执行回调代码。这种异步事物的问题在于,现在要使用此_id,您还必须使所有代码都异步。
因此,您将需要存储一个用mongodb中的_id
解析的Promise,并创建一个方法getId
来返回此承诺,如下所示:
private __id: Promise<object>
constructor(...) {
// ...
if(_id) {
this.__id = new Promise((resolve,reject) => {
Users.findOne({ _id },(err,user) => {
if(err) return reject(err)
resolve(user._id)
});
})
} else {
// Same here
}
}
public getId() {
return this.__id;
}
然后使用它:
const cursor = new UserCursor(...)
cursor.getId().then(_id => {
// Do smth with _id
})
或
async function doStuff() {
const cursor = new UserCursor()
const _id = await cursor.getId()
}
doStuff()
如果您现在在某个函数中执行此操作,则还必须使该函数async
您也可以像现在一样留下一个吸气剂,它将返回一个承诺,但我发现它比getId()
可读性差:
cursor._id.then(_id => { ... })
const _id = await cursor._id
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。