处理类中的回调

问题描述

我正在构建类以查找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