then / promise

Bare bones Promises/A+ implementation
https://www.promisejs.org
MIT License
2.58k stars 312 forks source link

Promise not being resolved #138

Closed AlexDenisov closed 7 years ago

AlexDenisov commented 7 years ago

Hi folks, I hope this is the right place to raise this concern.

On my project, I've found one nasty bug, which I didn't manage to fix myself. But, I was able to extract it into a small project.

Here is the code I have:

var monk = require('monk');
var promise = require('promise');

function connect() {
  return new promise( function (resolve, reject) {
    monk('mongodb://localhost:27017/monk', function (error, db) {
      if (error) {
        console.log('about to reject');
        reject(error); 
      } else { 
        console.log('about to resolve');
        resolve(db);
      }
    });
  });
}

connect()
  .then( function (db) {
    console.log(db); /// <--- Not getting here ever
  })
  .catch( function (error) {
    console.log(error);
  });

When I run it I see the about to resolve message, however, the following then never called.

So far I worked the issue around by using a wrapper object:

resolve({ database: db });

which works as expected.

How to reproduce

git clone https://gist.github.com/45f63dc3981b56de594e3d1da8c6305f.git monk_promise
cd monk_promise
npm install
node index.js

I hope this report helps to make this project better. Please, let me know if I need to provide more information.

ForbesLindesay commented 7 years ago

This is behaving as intended. monk already provides a promise based interface, so you could just do Promise.resolve(monk('mongodb://localhost:27017/monk')) to get a promise using this implementation, or monk('mongodb://localhost:27017/monk') if you're happy with node.js's built in promise implementation.

There is a bug in monk here because it does not check for the cases where the connection is already open or has already failed to open. If that bug was fixed, it would then be working fine with promise.

AlexDenisov commented 7 years ago

Hi @ForbesLindesay,

This is behaving as intended.

Could you tell me more about this? For me, it looks a bit weird since the program just hangs.