Hello
I hope someone can help me with this not necessarily Meteor related question. I’ve been having a difficult time keeping up with all the rapid changes within the JS space; in particular when it comes to the asynchronous patterns. There’s Meteor.wrapAsync, Future, Promise and Async/Await… I honestly don’t even know where to begin, so I’ll start with an example…
createProMember (customer) {
if (Meteor.isServer) {
check(customer, {
plan: String,
token: String
})
// Create a Future that we can use to confirm successful account creation.
const newCustomer = new Future()
const user = Meteor.user()
const userEmail = getEmail(user._id)
const isCustomer = user.profile.pro
if (!user) {
throw new Meteor.Error('500', 'You must be a registered user.')
} else if (isCustomer) {
throw new Meteor.Error('500', "You're already a customer.")
}
// Create our customer.
Meteor.call('stripeCreateCustomer', customer.token, userEmail, (error, stripeCustomer) => {
if (error) {
console.log(error)
} else {
const stripeId = stripeCustomer.id
const plan = customer.plan
// Setup a subscription for our new customer.
Meteor.call('stripeCreateSubscription', stripeId, plan, (error, response) => {
if (error) {
console.log(error)
} else {
try {
// Perform an update on our new user.
Meteor.users.update(user, {
$set: {
'profile.pro': {
stripeId: stripeId,
subscription: {
plan: {
name: customer.plan,
used: 0
},
payment: {
card: {
type: stripeCustomer.sources.data[0].brand,
lastFour: stripeCustomer.sources.data[0].last4
},
nextPaymentDue: response.current_period_end
},
status: response.status,
ends: response.current_period_end
}
}
}
}, (error, response) => {
if (error) {
console.log(error)
} else {
// Once the subscription data has been added, return to our Future.
newCustomer.return(user)
}
})
} catch (exception) {
newCustomer.return(exception)
}
}
})
}
})
// Return our newCustomer Future.
return newCustomer.wait()
}
}
How should this be coded so as to be inline with the Meteor/JS best practices of 2019.