KyleAMathews / superagent-bluebird-promise

Add promise support to superagent using Bluebird
MIT License
183 stars 37 forks source link

Warning: a promise was created in a handler but was not returned from it #52

Open aayush-practo opened 8 years ago

aayush-practo commented 8 years ago

I have updated sails to v0.12.1, node@4.2.3, npm@2.14.7 and using sails-mysql@0.11.3 adapter, superagent-bluebird-promise@3.0.0. For a promise like:

Model1.findOne().then(function (m1){
    var condition
     // some checks
     if (condition === true){
          Model1.update({}).then(function (m2){
               return res.json(200, {updatedValue: m2[0]}) 
         }).catch(function (error){
              return res.json(400, {error: error.message})
         })
     }
}).catch(function (error){
    return res.json(400, {error: error.message})
})

I am getting warning

Warning: a promise was created in a handler but was not returned from it
    at [object Object].Deferred.toPromise (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:494:61)
    at [object Object].Deferred.then (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:505:15)
    at donePayload (/home/vagrant/www/consumer-payments/api/controllers/v1/PaymentController.js:265:18)
    at /home/vagrant/www/consumer-payments/node_modules/sails/node_modules/async/lib/async.js:52:16
    at /home/vagrant/www/consumer-payments/node_modules/sails/node_modules/async/lib/async.js:269:32
    at /home/vagrant/www/consumer-payments/node_modules/sails/node_modules/async/lib/async.js:44:16
    at /home/vagrant/www/consumer-payments/api/controllers/v1/PaymentController.js:228:22
From previous event:
    at [object Object].Deferred.then (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:505:27)
    at Object.request (/home/vagrant/www/consumer-payments/api/controllers/v1/PaymentController.js:193:8)
    at wrapper (/home/vagrant/www/consumer-payments/node_modules/lodash/index.js:3095:19)
    at routeTargetFnWrapper (/home/vagrant/www/consumer-payments/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/home/vagrant/www/consumer-payments/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /home/vagrant/www/consumer-payments/node_modules/sails/lib/router/bind.js:187:7
    at done (/home/vagrant/www/consumer-payments/api/policies/v1/isAuthorizedProduct.js:29:25)
    at /home/vagrant/www/consumer-payments/api/services/authentication.js:22:22
    at Object.calculateHash (/home/vagrant/www/consumer-payments/api/services/hashing.js:8:12)
    at Object.wrapper [as calculateHash] (/home/vagrant/www/consumer-payments/node_modules/lodash/index.js:3095:19)

However, adding a return true at the end of first promise, does not give the warning.

Model1.findOne().then(function (m1){
    var condition
     // some checks
     if (condition === true){
          Model1.update({}).then(function (m2){
               return res.json(200, {updatedValue: m2[0]}) 
         }).catch(function (error){
              return res.json(400, {error: error.message})
         })
     }
     return true
}).catch(function (error){
    return res.json(400, {error: error.message})
})

Is there any other possible way to suppress the warning?

ckiss commented 8 years ago

I have this same issue. My code is this: request.get(/api/interval/${from}/${to}) .then(res => { return dispatch({ type: 'ACTION_RES', payload: res.body }) }, error => { return dispatch({type: 'ACTION_RES', error: true, payload: error}) })

Is there a way to suppress that warning?

jamesstidard commented 8 years ago

I'm having the same issue using a get as well:

const sign_out  = s => request.get(s.url).withCredentials().promise()
const sign_outs = services.map(s => sign_out(s).reflect())
Promise.all(sign_outs)
       .each(i => console.log(i.isFulfilled()))
LeeKevin commented 8 years ago

These issues are addressed at http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it.

After your nested calls, return null to signal that you are aware that you are not returning created promises in the handler.

dehli commented 7 years ago

I'm still getting this issue even with return null in the following code. I'll try to investigate.

return fetchUsersService(token)
    .then(response => {
        dispatch(successActionCreator(response.body));
        return null;
    });
AlexChalk commented 6 years ago

I have the same issue as @dehli