graphql - Apollo Server:如何根据回调发送响应?
问题描述
我目前正在尝试使用此包验证应用内购买的 iOS 收据:https ://github.com/Wizcorp/node-iap
这是我不完整的解析器:
export default {
Query: {
isSubscribed: combineResolvers(
isAuthenticated,
async (parent, args, { models, currentUser }) => {
const subscription = await models.Subscription.find({ user: currentUser.id });
const payment = {
...
};
iap.verifyPayment(subscription.platform, payment, (error, response) => {
/* How do I return a response here if it is async and I don't have the response object? */
});
}
),
},
};
如果它是异步的并且我没有响应对象,如何在此处返回响应?通常,我只是习惯于返回模型返回的任何内容。但是,这次我使用node-iap
的是基于回调的。
解决方案
你可以使用一个承诺:
const response = await new Promise((resolve, reject) => {
iap.verifyPayment(subscription.platform, payment, (error, response) => {
if(error){
reject(error);
}else{
resolve(response);
}
});
});
推荐阅读
- java - java从unicode码点获取unicode表示字符串
- vue.js - Vue如何在同一组件中调用模板
- docker - 创建 Docker 容器副本时如何配置端口
- xaml - UWP 应用程序中的自定义 MapIcon 图像大小错误
- json - 使用重复键 Powershell 遍历 JSON
- binary - 如何从二进制文件中提取十进制数字?
- django - 使用 AWS Elastic Beanstalk 部署 Django 应用程序时出现 502 Bad Gateway nginx/1.20.0 错误
- python-3.x - 没有安装带有“民意调查”标签的应用程序 - 激活模型 - 错误
- python - Scipy solve_ivp RuntimeWarning:在true_divide中遇到除以零
- python - 使用 python 获取网页内容失败,而是返回 FortiADC 错误页面