diff --git a/index.js b/index.js index 4755900..850b692 100644 --- a/index.js +++ b/index.js @@ -443,8 +443,28 @@ function callHandlers(handlers, args, method) { for (var i = handlers.length - 1; i >= 0; i -= 1) { method = handlers[i].bind(null, method, args); } + + //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(); + } + //start running the chain. - var promise = method(); nodify(promise, callback); return promise; }