Apollo Server:如何根据回调发送响应?
Apollo Server: How can I send a response based on a callback?
我目前正在尝试使用此软件包验证 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? */
});
}
),
},
};
如果响应是异步的并且我没有响应对象,我如何 return 在此处响应?通常,我只是习惯 returning 任何模型 returns。但是,这次我使用的是 node-iap
,它是基于回调的。
您可以使用 Promise:
const response = await new Promise((resolve, reject) => {
iap.verifyPayment(subscription.platform, payment, (error, response) => {
if(error){
reject(error);
}else{
resolve(response);
}
});
});
我目前正在尝试使用此软件包验证 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? */
});
}
),
},
};
如果响应是异步的并且我没有响应对象,我如何 return 在此处响应?通常,我只是习惯 returning 任何模型 returns。但是,这次我使用的是 node-iap
,它是基于回调的。
您可以使用 Promise:
const response = await new Promise((resolve, reject) => {
iap.verifyPayment(subscription.platform, payment, (error, response) => {
if(error){
reject(error);
}else{
resolve(response);
}
});
});