Skip to content

bluebird circular promise error while q-promise works #682

Description

@Ajkcki

The following code uses q-library to wrap PouchDB as a promise

var Promise = require("q");
var PouchDB = require("pouchdb");
var PouchdbPromise = (function () {
    function PouchdbPromise(pouchdbName) {
        this._pouchdbP = null;
        this.pouchdbName = pouchdbName;
    }
    PouchdbPromise.prototype.getPouchdb = function () {
        var self = this;
        if (self._pouchdbP !== null)
            return self._pouchdbP;
        var deferred = Promise.defer();
        try {
            var _pouchdb = new PouchDB(self.pouchdbName);
            deferred.resolve(_pouchdb);
        }
        catch (e) {
            deferred.reject("Pouchdb init error:" + e);
        }
        self._pouchdbP = deferred.promise;
        return self._pouchdbP;
    };
    return PouchdbPromise;
})();

var pdbP = new PouchdbPromise("abc");
pdbP.getPouchdb().then(function (abc) {
    abc.get("1");
});

It works fine. But switching to bluebird by changing the first line to

var Promise = require("bluebird")

leads to the following error:

Unhandled rejection TypeError: circular promise resolution chain

    See http://goo.gl/LhFpo0

    at process._tickCallback (node.js:415:13)
From previous event:
    at PouchdbPromise.getPouchdb (SOMEDIR/app/scripts/playground/bluebird2.js:20:22)
    at Object. (SOMEDIR/app/scripts/playground/bluebird2.js:31:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

the error was thrown at line 20 deferred.resolve(_pouchdb).

I know Promise.defer() is depreciated, using new Promise gets the same error.

I also tried to run the code in a browser (Chrome 43), bluebird throws the same error and both q and Chrome's native promise work fine. I'm using bluebird version 2.9.30

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions