如何推广大脑树方法? [英] How to promisify a braintree method?

查看:100
本文介绍了如何推广大脑树方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在推广大脑树方法时遇到了麻烦.具体而言,gateway.transaction.sale. https://developers.braintreepayments.com/reference/request/transaction/sale/node

I'm having trouble promisifying a braintree method. Specifically, gateway.transaction.sale. https://developers.braintreepayments.com/reference/request/transaction/sale/node

我正在将bluebird库与node.js配合使用.

I am using node.js with the bluebird library for promisification.

    ...
    var sale = bluebird.promisify(gateway.transaction.sale);
    return sale({
        amount: '10.00',
        paymentMethodNonce: nonce,
    });
})
.then( // doesn't reach here)
.catch(// logs out error)

具体地说,promise链底部的.catch块注销:

Specifically, the .catch block at the bottom of the promise chain logs out:

[TypeError: this.create is not a function]

当不尝试承诺时,代码可以正常工作.

When not attempting to promisify, the code works fine.

gateway.transaction.sale({
    amount: '10.00',
    paymentMethodNonce: nonce,
}, function(err, result) {
    ... no errors, everything works fine
}

脑树库的实现方式是否有问题?我说错了吗?我可以尝试其他替代策略来避免回调地狱吗?

Is this a problem with how the braintree library is implemented? Am I promisifying wrong? Are there any alternative promisification strategies I can try so I can avoid callback hell?

推荐答案

您缺少上下文参数,也称为this.如果您使用的是bluebird v2,请执行以下操作:

You're missing the context parameter, aka this. If you're using bluebird v2, do this:

var sale = bluebird.promisify(gateway.transaction.sale, gateway.transaction);

如果使用版本3,请执行以下操作:

If you use version 3, do this:

var sale = bluebird.promisify(gateway.transaction.sale, {context: gateway.transaction});

您可以在 Bluebird的文档中看到它.

这篇关于如何推广大脑树方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆