Search code examples
node.jstypescriptcouchdbbluebirdcouchdb-nano

Bluebird promises in NodeJS, not getting to then


I'm attempting to use bluebird promises in NodeJs with nano a library used with couchDb. I use the promisfy and when I look get the new async methods. In the following example, the nano.db.listAsync call works out fine, but I never get to the .then or the .catch.

What is wrong here?

   var nano = require('nano')(this.appInfo.dbServiceUrlPrefix);
        Promise.promisifyAll(nano);
        Promise.promisifyAll(nano.db);

       var p = nano.db.listAsync(function(err,body) {
            // get all the DBs on dbServiceUrlPrefix
            var dbNames:string[] = <string[]> body ;
            console.log("allDbs",dbNames) ;
            return dbNames ;
        }).then(function (e:any) {
            console.log('Success',e);
        }).catch(function(e:any){
            console.log('Error',e);
        });


Solution

  • There are a couple things wrong.

    1. After promisification and calling the promsified version, you use .then() to get the result.
    2. The .then() resolve handler has no err variable any more. If there's an error, the .then() reject handler is called.

    So, I think you want something like this:

       var nano = require('nano')(this.appInfo.dbServiceUrlPrefix);
       Promise.promisifyAll(nano);
       Promise.promisifyAll(nano.db);
    
       nano.db.listAsync().then(function(body) {
            // get all the DBs on dbServiceUrlPrefix
            var dbNames:string[] = <string[]> body ;
            console.log("allDbs",dbNames) ;
            return dbNames;
        }).then(function (e:any) {
            console.log('Success',e);
        }).catch(function(e:any){
            console.log('Error',e);
        });
    

    P.S. Are you sure there are not supposed to be any function arguments passed to nano.db.listAsync()?