diff --git a/packages/node_modules/pouchdb-wrappers/lib/index.js b/packages/node_modules/pouchdb-wrappers/lib/index.js index dd52871d..89bd107b 100644 --- a/packages/node_modules/pouchdb-wrappers/lib/index.js +++ b/packages/node_modules/pouchdb-wrappers/lib/index.js @@ -463,8 +463,26 @@ function callHandlers(handlers, args, method) { for (var i = handlers.length - 1; i >= 0; i -= 1) { method = handlers[i].bind(null, method, args); } - //start running the chain. - var promise = method(); + // start running the chain. + // make sure the handler chain is not started, before the corresponding + // pouchdb instance is fully initialized. Otherwise it might result + // in a double calling the augmented methods + var promise; + if (!args.base.taskqueue.isReady) { + promise = new Promise(function (resolve, reject) { + args.base.taskqueue.addTask(function (failed) { + if (failed) { + reject(failed); + } else { + resolve(); + } + }); + }).then(function () { + return method(); + }); + } else { + promise = method(); + } nodify(promise, callback); return promise; }