Search code examples
javascriptpromisebluebirdq

Want to return a promise based on a series of other promise completioen


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?


Solution

  • 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.