Skip to content

Promises

tabulardata edited this page Oct 25, 2013 · 3 revisions

Alongside the version 2 release we added support for promises. Every resource method now returns a Promises/A+ compliant promise in addition to supporting the conventional callback:

Using a regular callback:

stripe.customers.create({...}, function(err, customer) {...});

Using a promise:

stripe.customers.create({...})
  .then(function(customer){...}, function(err) {...});

Here's an example of creating a customer and then a new charge for that customer:

stripe.customers.create({
  email: 'foo-customer@example.com'
}).then(function(customer) {
  return stripe.charges.create({
    amount: 1600,
    currency: 'usd',
    customer: customer.id
  });
}).then(function(charge) {
  // New charge created on a new customer
}, function(err) {
  // Deal with an error
});
Clone this wiki locally