6
votes

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.

1
have you tried using a .then. so say you have $scope.connect = function(){ var req = {options}; $http(req).success(callback).error(callback).then(callback) } The .then callback function will not run until after all the data is successfully back and success and error have run. Check this out for more info peterbe.com/plog/promises-with-$http - Jess Patton
Thanks jess,okay then i need to put all code in then block which i want to execute after pouchdb connects. right? - Bhavesh Jariwala
yep, should work. Here is a example. db.get('mittens').then(function (doc) { // okay, doc contains our document }).catch(function (err) { // oh noes! we got an error }); - Jess Patton
Similar to angular it uses a promise system. You can read more about it here. pouchdb.com/guides/async-code.html - Jess Patton
Hope it works out. I use this method with mongodb but i think it should work - Jess Patton

1 Answers

1
votes

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