0

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);
        });
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
jeff
  • 3,269
  • 3
  • 28
  • 45

2 Answers2

2

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()?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

I think the function parameters you pass to nano.db.listAsync() are incorrect. It would not have err parameter after promissification, so your code should look something like this:

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

    var p = nano.db.listAsync(function(body) {
        ...
dtoux
  • 1,754
  • 3
  • 21
  • 38
  • I've tried that. it comes up null. if I look at the prototype, the listAsync takes arg1,arg2, arg3. when I tried these, the 2nd argument had the body, the third looked like the request. The first I suspect to be the err. – jeff May 01 '16 at 03:23