How to chain several Meteor.Calls so next one is triggered by succesful callback

How to chain Meteor calls so succesful Meteor Method triggers the next call?

1st Meteor.call // succesful Method triggers 2
2nd Meteor.call 2 // succesful Method triggers 3
3nd Meteor.call 3 //

Either nesting them inside callbacks like:

Meteor.call("method1", function(err, res){
    Meteor.call("method2", function(err, res){
        Meteor.call("method3", function(err, res){
    
        }
    }
}

Or use an async library like peerlibrary:async

async.series([
    function(callback){
        Meteor.call("method1", function(err, res){
            callback(null, true)
        });
    },
    function(callback){
        Meteor.call("method2", function(err, res){
            callback(null, true)
        });
    },
    function(callback){
        Meteor.call("method3", function(err, res){
            callback(null, true)
        });
    },
]);

I’d personally recommend using an async library as it’s a lot clearer and prevents callback hell.

3 Likes

@nlammertyn thx mate!! Gotta give it a try!