Warm tip: This article is reproduced from stackoverflow.com, please click
asynchronous javascript node.js

Promises and async/await combination, ideal way to wait for asynchronous operations?

发布于 2020-03-27 10:28:35

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?

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;
     });

     })

}

Questioner
d12
Viewed
66
T.J. Crowder 2019-07-03 23:29

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;
}