2

Heres my code:

var delete_card = function(){

  stripe.customers.deleteCard(
    this.service_id.stripe,
    this.credit_card[0].stripe_id
  )
  .then(function(obj){
    this.recipients.splice(0, 1);
    return this.save()
  })
}

Both the stripe call and the save call return promises.

How would I RETURN a promise from the delete_card method?

Would I wrap them all in like new Promise and return a promise from there?
How would I structure that so I bubble up both errors and results?

I want to be able to do something from the caller like this:

delete_card
.then(...)
.catch(...)

And just keep composing chained promises?

Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
  • "*Would I wrap them all in like new Promise and return a promise from there?*" - [**NOOO!**](http://stackoverflow.com/q/23803743/1048572) – Bergi Oct 10 '16 at 22:00
  • Hahahaha I read some best practices that said that was a bad idea. zero's answer below was exactly what I was looking for and it makes sense too. – Dr. Chocolate Oct 10 '16 at 22:24

1 Answers1

5

Just return the promise in the delete_card function.

var delete_card = function(){

  /************/
  /**/return/**/ stripe.customers.deleteCard(
  /************/
    this.service_id.stripe,
    this.credit_card[0].stripe_id
  )
  .then(function(obj){
    this.recipients.splice(0, 1);
    return this.save()
  })
}

Edit: I made it obnoxiously obvious where to add the return since the code difference is so subtle.

zero298
  • 25,467
  • 10
  • 75
  • 100
  • YES. Thank you. I kept trying to retur the `this.save()` and getting an undefined `then` error. Then I noticed you were returning the entire chain at the top: `return stripe....` It works! Thank you! Promises are so nice, I cant even look at a callback again. – Dr. Chocolate Oct 10 '16 at 22:22
  • @NickKiermaier Ha, I made it a bit more obvious where the difference is in case someone else comes and can't tell what I changed. Sorry about that. – zero298 Oct 11 '16 at 15:17