我正在设置一个 GraphQL 解析器来调用 Braintree 端点。Braintree npm 包希望使用如下代码调用其端点:
braintreeGateway.customer.create({
firstName: "Jen",
lastName: "Smith",
company: "Braintree",
email: "jen@example.com",
phone: "312.555.1234",
fax: "614.555.5678",
website: "www.example.com"
}, function (err, result) {
result.success;
result.customer.id;
});
GraphQL 解析器返回承诺。我试图弄清楚如何承诺这个回调,并将它包含在一个承诺解析器中。
我已经阅读了很多关于承诺回调的 SO 帖子,但到目前为止我发现的那些似乎与这个用例不太匹配。
我已经尝试了很多东西,这是最新的:
getBrainTreeCustomerId: (parent, args, context) => {
const userid = context.userId;
const braintreeCustomerCreate = util.promisify(braintreeGateway.customer.create);
async function run_braintreeCustomerCreate() {
try {
let braintreeCustomerId = await braintreeCustomerCreate({
firstName: "Jen",
lastName: "Smith",
company: "Braintree",
email: "jen@example.com",
phone: "312.555.1234",
fax: "614.555.5678",
website: "www.example.com"
});
return braintreeCustomerId
}
catch (err) {
console.log('ERROR:', err);
}
}
return Promise.resolve()
.then(() => {
let braintreeCustomerId = (async () => {
let braintreeCustomerId = await run_braintreeCustomerCreate()
return braintreeCustomerId;
})();
return braintreeCustomerId;
})
.then((braintreeCustomerId) => {
return braintreeCustomerId;
})
.catch((err) => {
console.log(err);
});
}
}
但是 catch 处理程序收到一条错误消息,提示“无法读取未定义的属性 '_createSignature'”。
在这里使用的正确语法是什么?