Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait till PouchDB successfully connects

I am using pouchdb at client side(ionic mobile app) couchdb at server side.

I need to perform operation after pouchdb successfully created and sync with couchdb.

so how can I wait till pouchdb complete initial activity.then after only my client side execution should start.

currently pouch is working on asynchronous manner so sometime before pouch initialize my application starts execution and I am getting error for pouchdb.

like image 535
Bhavesh Jariwala Avatar asked May 11 '15 04:05

Bhavesh Jariwala


1 Answers

When working with asynchronous functions such as waiting for a respone from a server in JavaScript you use promises or callbacks to wait for an answer.

from the pouchdb docs we can read that they provide a fully asynchronous API.

Callback version:

db.get('mittens', function (error, doc) {
  if (error) {
    // oh noes! we got an error
  } else {
    // okay, doc contains our document
  }
});

Promise version:

db.get('mittens').then(function (doc) {
  // okay, doc contains our document
}).catch(function (err) {
  // oh noes! we got an error
});
like image 124
eikooc Avatar answered Jan 02 '23 11:01

eikooc