简体   繁体   中英

Trying to use Async and Await from different functions

While I thought I was using Async and Await keywords properly, it appears as though I am doing something wrong.

I am using PouchDB, which is a great Javascript database that syncs with CouchDB. PouchDB uses promises for many of its functions, and has native support for Async and Await. As an example, to retrieve the basic information about a database, you would use the following code:

    db.info().then(function (info) {
  console.log(info);
})

I understand this, and it works. But if I try and put this inside a function, and then call that function, things go haywire. And I'm sure it's me and not PouchDB that is the problem...

function getLocalDBInfo(db){

  try {
    db.info().then(function (info) {
      return info;
    });
  } catch (error) {
    console.log("can't get local DB info: ", error);
  }
}

async function testing(db){
  try {
    var info=await getLocalDBInfo(db);
    await console.log("DB info=", info);
    await console.log("Doc count= ", info.doc_count);
  }
  catch(err){
    console.log("error=",err);
  }

  //info contains...
    //{"doc_count":0,"update_seq":0,"db_name":"kittens"}
}

testing(MY_db);

If I log info inside the getLocalDBInfo function, it eventually (ie. after the promise fulfills) logs info to the console. But, the logs inside the testing function return immediately with undefined . It makes sense to me that they are returning undefined because they are returning immediately, but I was trying to have them wait for info by using async and await . Any suggestions as to what I am doing wrong?

getLocalDBInfo() is not returning a promise, so you can't await for it.

with async/await you could:

async function getLocalDBInfo(db){
    try{
       return await db.info()
    } catch (err){
       console.log("can't get local DB info: ", error);
    }
}

you can also use new promise

getLocalDBInfo generates a promise, attaches a callback to it, then returns undefined . It doesn't return the resolved promise and you can't await on an undefined (well, you can, but it won't do anything).

You neee to change getLocalDBInfo to actually return the promise.

Also, why do you have a then block with the signature info => info ? All this will do is unwrap the async value and then wrap it again. You can omit the then callback entirely.

Similarly, are you sure you need to wrap the db.info call in a try/catch? I wonder if you might be intending to use a promise.catch here instead. The try/catch construct only grabs promise rejections when awaiting an expression.

You need to return the value from the outer function getLocalDBInfo

function getLocalDBInfo(db) {
  return new Promise((resolve, reject) => {
    db.info()
      .then(info => resolve(info))
      .catch(err => reject(err))
  })
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM