Richard G Richard G - 4 years ago 156
Javascript Question

how to wrap a promise with bluebird

I'm using bluebird for promises, but also using a library that returns a non-bluebird promise. I want to use

. I tried to use
to wrap it, which I found somewhere else online, but it hides the errors in the promise chain. In the code if I take out the
it resolves rather than throwing an error from the client call, even though an error happened.

Besides creating a new promise with
new Promise(resolve,reject)
, which is an obvious solution, is there a better way to convert it to a bluebird promise, one that will propagate any errors to the original promise chain?

module.exports.count = function(params, done){
var promise = client.count({
"index": + "_" + params.index
return Promise.resolve(promise).asCallback(done);

Answer Source

Promise.resolve does propagate errors. Your problem seems to be that catch handles them before they could reach the resolve. You should be doing

function count(params, done){
  return Promise.resolve(client.count({
    "index": + "_" + params.index
    return response; // important!
  }, function(e){
    throw e; // important!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download