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. Is this the ideal way to wait for the completion of asynchronous operations?
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;
});
});
})
}
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. (And the callback you give to a non-promise-focussed function should almost never be an async
function.)
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
. MongoDB has those available in its JavaScript API now (I'm afraid I don't have the details). Then you'd use an async
function with await
, something like this (according to this blog post ):
// 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.