来自文档(Mongoose v5.4.1,最新版本):
Mongoose async operations, like .save() and queries, return thenables. This means that you can do things like MyModel.findOne({}).then()
文档中的第二段说明:
Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience.
什么 Javascript MDN 网页声明:
The then() method returns a Promise.
这是否意味着 mongoose 对异步函数有另一种实现,他们为异步操作的结果保留了 then 关键字?
换句话说,它们表现得像 promise 但不是 JS promises?
最佳答案
来自documentation :
Mongoose queries are not promises. They have a
.then()
function for co and async/await as a convenience. However, unlike promises, calling a query's.then()
can execute the query multiple times.
因此,与实际的 promise 不同,如果您在查询中多次调用 then()
,您实际上会多次执行查询(或更新)。
如果您想要一个实际的 promise ,请调用 exec()
关于查询。
let promise = Test.findOne({}).exec();
关于javascript - Mongoose promise 文档说查询不是 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53970784/