在balanced.marketplace.customers.create
后台执行网络调用并返回一个承诺,这意味着访问资源上的底层数据,您将不得不使用.then
var customer = balanced.marketplace.customers.create(...);
customer.then(function(c) {
console.log(c.href);
});
这可能让您感到困惑的原因是平衡节点库使用的承诺是“超载的”,因为您可以将操作串在一起。仅.then
当您想要访问承诺的结果时才需要使用。这意味着您可以执行以下操作:
var card = balanced.get('/cards/CCasdfadsf'); // this is a network call
var customer = balanced.marketplace.customers.create(); // this is a network call that will go in parallel
card.associate_to_customer(customer).debit(5000); // using promises these will complete once all the previous request are complete
customer.then(function (c) {
console.log(c.href); // since we need to access the data (href) on the customer, we have to wait for the non blocking requests to complete and then the data will be ready.
});