asynchronous - Trouble to synchronise promises in Node.js using Q -


i doing api in node.js framework sails.js. using promises first time , have troubles sync promises want.

my main function following :

createcard: function(req, res) {     checkifuserhasstripeaccount(req.user)         .then(addcreditcardtostripeaccount())         .then(function cardcreated() {             res.send(200, {                 msg: 'card created'             });         })         .catch(function handleerror(err) {             res.send(err.httpcode, err.msg);         }) }, 

obviously can't add credit card stripe account if user doesn't have one.

the function checkifuserhasstripeaccount() checks if account exists , if not, create it.

here code part :

function checkifuserhasstripeaccount(user) {     var deferred = q.defer();      if (!user.idstripe) {         createstripeaccounttouser(user)             .then(function(saveduser) {                 deferred.resolve(saveduser);             })             .catch(function(err) {                 deferred.reject(err);             })     } else {         deferred.resolve(user);     }     return deferred.promise; }  function createstripeaccounttouser(user) {     var deferred = q.defer();      var jsonusertocreate = {         description: user.firstname + ' ' + user.surname,         email: user.email     };      stripe.customers.create(jsonusertocreate, function(err, customer) {         if (err) {             deferred.reject({                 httpcode: 500,                 msg: 'some error'             });         } else {             user.idstripe = customer.id;             user.save(function(err, saveduser) {                 if (err) {                     deferred.reject({                         httpcode: 500,                         msg: 'some error'                     });                 }                 deferred.resolve(saveduser);             });         }     });      return deferred.promise; } 

the problem .then(addcreditcardtostripeaccount()) executed before checkifuserhasstripeaccount() finished.

i can't figure out why. thought .then(addcreditcardtostripeaccount()) executed if received reject or resolve.

you correct in line of thought. problem invoking function instead of referencing it:

.then(addcreditcardtostripeaccount()) 

should be:

.then(addcreditcardtostripeaccount) 

i expect work:

createcard: function (req, res) {     checkifuserhasstripeaccount(req.user)     .then(addcreditcardtostripeaccount)     .then(function cardcreated(){         res.send(200, {msg: 'card created'});     })     .catch(function handleerror(err) {         res.send(err.httpcode, err.msg);     }) }, 

for future, note () after function name invokes function, order of execution in js evaluate first due being inside then's ().

in promise chains, invoke first function. example:

function first () { /*...*/ } // return promise. function second() { /*...*/ } function third () { /*...*/ }  first() // invoked     .then(second) // not invoked. second() have been bad here.     .then(third); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -