承诺 Stripe API

Ole*_*Ole 1 javascript node.js promise stripe-payments es6-promise

我正在尝试util.promisify以下条纹调用,该调用确实成功了:

stripe.customers.create(
  {
    description: 'My First Test Customer (created for API docs)',
  },
  function(err, customer) {
      console.log(customer)
  }
)

Run Code Online (Sandbox Code Playgroud)

IIUC 这应该有效:

const util = require('util')

const createCustomerPromise = util.promisify(stripe.customers.create)

createCustomerPromise(
{
    description: 'My First Test Customer (created for API docs)'
}
).then(customer=>console.log(customer))

Run Code Online (Sandbox Code Playgroud)

但是,当我运行上面的命令时,我得到:

(node:28136) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'createResourcePathWithSymbols' of undefined
    at /home/ole/Temp/stripetest/node_modules/stripe/lib/StripeMethod.js:27:12
    at internal/util.js:286:30


Run Code Online (Sandbox Code Playgroud)

Nik*_*des 5

Stripe 的 Node SDKstripe-node已经返回 Promises,因此您无需对其进行 promisify

来自文档

每个方法都会返回一个可链接的 Promise,可以使用它来代替常规回调:

只需省略错误优先回调

stripe.customers.create({
  description: 'My First Test Customer (created for API docs)'
})
.then(result => console.log(result))
Run Code Online (Sandbox Code Playgroud)

或使用async/await

const result = await stripe.customers.create({
  description: 'My First Test Customer (created for API docs)'
})
console.log(result)
Run Code Online (Sandbox Code Playgroud)