我正在尝试在 Stripe 中创建新客户。我很成功,但对他们的文档如何设置函数调用的样式感到困惑。
我似乎找不到有关其官方文档的任何信息。https://stripe.com/docs/api/customers/create?lang=node
例如:
stripe.customers.create({
description: 'Customer for jenny.rosen@example.com',
source: "tok_visa" // obtained with Stripe.js
}, function(err, customer) {
// asynchronously called
});
我假设它类似于".then((err, customer( =>{}',但似乎不能用这种语法使用函数调用。
任何解释都会有所帮助!
知道的是承诺,它们是当今异步的首选方式。Stripe 的 API 使用的是回调(也称为 errback(样式,该样式早于 Promises。
它类似于
.then(customer => ...).catch(err => ...)
但是,Stripe 的节点库也会返回 promise,因此您可以将示例转换为:
stripe.customers.create({
description: 'Customer for jenny.rosen@example.com',
source: "tok_visa" // obtained with Stripe.js
})
.then(customer => ...)
.catch(err => ...);
号的含义与任何其他函数调用中的含义相同。它分隔参数。
第二个参数恰好是一个函数。
var first_argument = {
description: 'Customer for jenny.rosen@example.com',
source: "tok_visa" // obtained with Stripe.js
};
var second_argument = function(err, customer) {
// asynchronously called
};
stripe.customers.create(first_argument, second_argument);