But I want the calls to be synchronous, meaning that I want showId and showName to be populated with the results from the first Method call to insert show. How do I do this? I know I can bundle the methods into one server method call, but I would prefer to keep insertEpisode and insertShow separate. I’ve been looking at Meteor.wrapAsync() and Fibers all day today, but they seem to only be server-side so that doesn’t help. Any tips?
I like to use meteor promises. Here is an example case I used it in: creating a new user, registering their card with stripe, and creating an initial order.
Thank you so much Steve! Took me quite a while to figure this out. I see your point in that other post too about the Meteor.call’s in terms of latency compensation. For calls like this …
insertShow: function(attributes) {
// author
var author = Meteor.user();
// extend attributes
var show = _.extend(attributes, {
authorId: author._id,
authorName: author.profile.name,
});
/*
* Check if Exists
*/
// - If it does exist, don't return error, just don't insert new document.
// - This is to allow for several episodes from the same show.
var showExistsId = Shows.findOne({uniqueShowId: show.uniqueShowId}, {fields: {'_id': 1, 'name': 1}});
// Show exists already
if(showExistsId) {
return {
_id: showExistsId,
name: showExistsId.name,
exists: true
};
}
var showId = Shows.insert(show, function(error, result) {
// throw error
if(error) {
throw new Meteor.Error(error.error, error.message);
}
});
// success
return {
_id: showId,
name: show.name
};
}
… are these recommended to execute client-side? Eg. not calling for a server method? And Thanks so much corvid, I’ve been looking at Promise and will definitely use it once the method callback stack becomes a bit more unvielding.
For anyone looking into this in the future I highly suggest taking a look at the Meteor.call() and Meteor.apply() documentation. Somehow after frantically Googling for two days I did not stumble upon this. Would love a bit more documentation / some more examples on this, for instance where is returnStubValue: true explained / documented?