[英]Promises and async/await combination, ideal way to wait for asynchronous operations?
Having async/await by itself in a function would return a "pending value" but if a promise used then the actual value will be returned in the end. 在函数中单独具有async / await会返回“待处理的值”,但是如果使用了promise,则最终将返回实际值。 Is this the ideal way to wait for the completion of asynchronous operations?
这是等待异步操作完成的理想方法吗?
This returns exactly what I want
这正是我想要的
var foundForm = await getDocument(query) //Returns the resulting document
async function getDocument(query){
return new Promise((resolve,reject) =>{
MongoClient.connect (url, async function(err, db) {
if (err) throw err;
console.log(query)
db.collection("users").find(query).toArray(function(err, result) {
if (err) {
console.log(err)
throw err;
}
console.log(result);
db.close();
resolve(result) // returns result;
});
});
})
}
This doesnt return what I need:
这不会返回我需要的东西:
var foundForm = await getDocument(query) //Returns 'pending'
async function getDocument(query){
MongoClient.connect (url, async function(err, db) {
if (err) throw err;
console.log(query)
db.collection("users").find(query).toArray(function(err, result) {
if (err) {
console.log(err)
throw err;
}
console.log(result);
db.close();
return result;
});
})
} }
Since your getDocument
code needs to wait for an asynchronous operation that doesn't provide a Promise interface, getDocument
shouldn't be an async
function, because you need to create the promise manually. 由于您的
getDocument
代码需要等待不提供Promise接口的异步操作,因此getDocument
不应是async
函数,因为您需要手动创建promise。 (And the callback you give to a non-promise-focussed function should almost never be an async
function.) (而且您提供给非承诺函数的回调几乎永远不会是
async
函数。)
function getDocument(query){
return new Promise((resolve,reject) =>{
MongoClient.connect(url, function(err, db) {
if (err) {
// Reject, don't throw
reject(err);
return;
}
console.log(query);
db.collection("users").find(query).toArray(function(err, result) {
if (err) {
// Reject, don't throw
reject(err);
return;
}
console.log(result);
db.close();
resolve(result);
});
});
});
}
Alternately, use promise-enabled versions of MongoClient.connect
and db.collection("users").find
. 或者,使用启用了承诺的
MongoClient.connect
和db.collection("users").find
。 MongoDB has those available in its JavaScript API now (I'm afraid I don't have the details). MongoDB现在在其JavaScript API中提供了这些功能(恐怕我没有详细信息)。 Then you'd use an
async
function with await
, something like this (according to this blog post ): 然后你会使用一个
async
与功能await
,(根据这样的 这篇博客文章 ):
// BE SURE TO DOUBLE-CHECK THE DETAILS
async function getDocument(query){
const db = await MongoClient.connect(url);
const await result = db.collection("users").find(query).toArray();
console.log(result);
await db.close(); // No idea whether you need `await` here or not
return result;
}
In short, yes. 简而言之,是的。 If you are targeting a platform that supports es6 async/await and want to utilize that, then if a library only exposes a callback api then "wrapping" the callback in a Promise (your first function) is how you'd accomplish that.
如果您要针对支持es6 async / await的平台并希望利用该平台,那么如果库仅公开了一个回调API,然后将其“包装”在Promise(您的第一个函数)中,便是您的实现方式。 Although you'd first want to make sure that the library doesn't offer Promise based api, then you could just return the Promise from the library and await that.
尽管您首先要确保该库不提供基于Promise的api,但是您可以从该库中返回Promise并等待它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.