I have a Meteor method that tries to manipulate the database, and if successful, calls an async method. I would like to be able to call this method and return the result of the async call or the error from the database manipulation.
This is (roughly) my code on the server:
Meteor.methods({
'data.update'(id, data) {
Collection.update({id_: id}, {$set: {data: data}}, error => {
if (error) {
// Have method return Meteor error for DB-failure
} else {
callAsync(id, (error, res) => {
if (error) {
// Have method return Meteor error for async call failure
} else {
// Have method return success(res)
}
})
}
})
}
});
I have read about Futures and Promises, but I'm new to the concepts and I'm not sure when to use what. Preferably I'm looking for a solution that does not rely on any third party libraries outside of Meteor/ES6. Bonus (related) question: what is normally returned after a database manipulation that let's me attach a callback to a method?