If I log the code execution to the server, I see that the callback fires immediately and logs [2.1] and [2.2] to the console, while the API [SUCCESS] response is logged only later, despite my assumption that it should awaited. What am I missing? Thank you very much!
React Native Client
try {
// 2.1 Set subscription status
Meteor.call('users.setSub', {
receipt: transactionReceipt,
...
}, (error, result) => {
if (error) {
...
} else {
// 2.2 Subscribe to plan from db
const planHandle = Meteor.subscribe('plan.sub', 'subscribed', async () => {
const subscription = await Meteor.user().sub;
const paid = await (subscription.expires > Date.now());
const plan = paid && await Meteor.collection('Plans').findOne({});
const kw = plan && plan.kw;
const trial = !paid;
const trialStage = subscription.trialStage;
const trialPlan = Meteor.user().tagesplan;
const { breakfast, lunch, dinner } = (trial && trialPlan) || (plan && plan[week][day]) || { breakfast: '', lunch: '', dinner: '' };
// 2.3 Update state with new plan and subscription status
this.setState({
plan,
breakfast,
lunch,
dinner,
loading: !breakfast || !lunch || !dinner,
trial,
trialStage,
transactionReceipt,
purchaseToken,
productId,
kw,
});
});
}
});
...
Server
'users.setSubscription': async function usersSetSubscription(params) {
...
await updatePlan({ owner: this.userId });
...
try {
...
// use JWT to authenticate with service account
await google.options({ auth: jwtClient });
// get subscription status by purchaseToken
await googlePlay.purchases.subscriptions.get({
packageName,
subscriptionId,
token: purchaseToken,
}, async (error, response) => {
if (error) {
console.log('[ERROR - GOOGLE PLAY: SUBSCRIPTIONS.GET] ', error);
} else {
console.log('[SUCCESS - GOOGLE PLAY: SUBSCRIPTIONS.GET] ', response);
// check if expired
const { data } = response; // JSON.parse(response);
const expiration = data.expiryTimeMillis;
subscription.active = await (expiration > Date.now());
subscription.expires = expiration;
if (!subscription.starts) subscription.starts = data.startTimeMillis;
// update user
return setSubscription({ userId: this.userId, subscription })
.then(response => response)
.catch((exception) => {
console.log('[ERROR][user.setSubscription] ', exception);
throw new Meteor.Error('500', exception);
});
}
}
...
}