如何承诺 braintree 方法?
How to promisify a braintree method?
我无法承诺 braintree 方法。具体来说,gateway.transaction.sale。 https://developers.braintreepayments.com/reference/request/transaction/sale/node
我正在使用 node.js 和 bluebird 库来实现承诺。
...
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块注销:
[TypeError: this.create is not a function]
当不尝试 promisify 时,代码工作正常。
gateway.transaction.sale({
amount: '10.00',
paymentMethodNonce: nonce,
}, function(err, result) {
... no errors, everything works fine
}
这是 braintree 库实现方式的问题吗?我承诺错了吗?是否有任何替代的 promisification 策略我可以尝试以避免回调地狱?
您缺少上下文参数,又名 this
。如果您使用的是 bluebird v2,请执行以下操作:
var sale = bluebird.promisify(gateway.transaction.sale, gateway.transaction);
如果您使用版本 3,请执行以下操作:
var sale = bluebird.promisify(gateway.transaction.sale, {context: gateway.transaction});
您可以在 Bluebird's docs 中看到它。
你可以像这样承诺大多数的 braintree 方法:
var G = {};
for(var k1 in gateway){
if(gateway.hasOwnProperty(k1) && typeof gateway[k1] === 'object'){
G[k1] = G[k1] || {};
for(var k2 in gateway[k1]){
if(typeof gateway[k1][k2] === 'function'){
console.log('Promisify:', k1, k2);
G[k1][k2] = G[k1][k2] || {};
G[k1][k2] = bluebird.promisify(gateway[k1][k2], {context: gateway[k1]});
}
}
}
}
那么你可以这样使用它们:
G.paymentMethod.find('payment_method_token').then(cb_suc).catch(cb_fail);
我无法承诺 braintree 方法。具体来说,gateway.transaction.sale。 https://developers.braintreepayments.com/reference/request/transaction/sale/node
我正在使用 node.js 和 bluebird 库来实现承诺。
...
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块注销:
[TypeError: this.create is not a function]
当不尝试 promisify 时,代码工作正常。
gateway.transaction.sale({
amount: '10.00',
paymentMethodNonce: nonce,
}, function(err, result) {
... no errors, everything works fine
}
这是 braintree 库实现方式的问题吗?我承诺错了吗?是否有任何替代的 promisification 策略我可以尝试以避免回调地狱?
您缺少上下文参数,又名 this
。如果您使用的是 bluebird v2,请执行以下操作:
var sale = bluebird.promisify(gateway.transaction.sale, gateway.transaction);
如果您使用版本 3,请执行以下操作:
var sale = bluebird.promisify(gateway.transaction.sale, {context: gateway.transaction});
您可以在 Bluebird's docs 中看到它。
你可以像这样承诺大多数的 braintree 方法:
var G = {};
for(var k1 in gateway){
if(gateway.hasOwnProperty(k1) && typeof gateway[k1] === 'object'){
G[k1] = G[k1] || {};
for(var k2 in gateway[k1]){
if(typeof gateway[k1][k2] === 'function'){
console.log('Promisify:', k1, k2);
G[k1][k2] = G[k1][k2] || {};
G[k1][k2] = bluebird.promisify(gateway[k1][k2], {context: gateway[k1]});
}
}
}
}
那么你可以这样使用它们:
G.paymentMethod.find('payment_method_token').then(cb_suc).catch(cb_fail);