For Meteor 1.3 server, what is good/reasonable way to handle async?


i have some server-side code for deleting a user that looks something like this (assume it’s being called from a Meteor.method() where i’ve already done all the security checks):

DeleteAccount = function(userId) {

the code in those methods is actually a little involved given the collections, but for simplification, let’s assume they look something like this;

DeleteBlogComments = function(userId) {
	Comments.remove({userId: userId});

DeleteBlogPosts = function(userId) {
	Posts.remove({userId: userId});

DeleteUserAccount = function(userId) {
	Meteor.users.remove({_id: userId});

i’m ok w/ the comments and posts deletion code run “at the same time”, but i don’t want to delete the actual user account until both those complete. currently, i have no promises or async implemented, but i would like to.

i’m most familiar w/ the $q library, and i see there is a Meteor package for it. but given what’s available in Meteor 1.3, ES6, and coming in ES7, what would be best practice right now?


A good Meteor way to go about this is with the collection hooks - a perfect use case is removing documents related to a deleted user account. No need to worry about promises in your situation.

Meteor.users.before.remove(function (userId, doc) {
   Posts.remove({userId: userId}, (err) => {
        if(err) { throw new Meteor.Error(err.message) }
   Comments.remove({userId: userId}, (err) => {
        if(err) { throw new Meteor.Error(err.message) }


thanks, @habitatmike, that is an excellent solution to my stated problem. and i’ll probably incorporate that into my solution.

as it turns out, i’ve over-simplified my issue. in reality, i have several more (non-user) collections to clean up, and some of those should not start until a previous one has finished.

in order to coordinate those, is it Promises i want to use, vs say some other async method? if so, is there a promises library that is popular in the community?


@zimv20 Using promises would be the most straight-forward approach, as it is designed for situations like the one you are explaining + no dependencies!

Otherwise, async is a very easy-to-use module with a lot of documentation.


I’m a little confused.

If these functions are run inside a method on the server they will run sequentially. Meteor’s non-callback Mongo methods run inside fibers and execution proceeds only when the preceding operation is complete.

In other words you don’t need to do anything.


@robfallows, you are right given my over-simplified examples. in the actual app, some of the remove()'s do have callbacks, and i need to coordinate among those.