Meteor: async update subscription

I have a subscription that, after calling ready() performs a series of updates pulling data from other collections:

 Meteor.publish('foo', function() { this.ready() // Several times: var extraData = OtherCollection.findOne(...) this.changed(..., extraData) }) 

How can I run these updates asynchronously? Each update accesses the database, performs some calculations, and calls changed in the subscription.

I also need to run the code after all updates are complete (resynchronization).

+7
javascript asynchronous meteor
source share
1 answer

Just save the publication handler and use it later!

 var publishHandler; Meteor.publish('foo', function() { publishHandler = this; //Do stuff... }); //Later, retrieve it and do stuff with it doSomeAsync(Meteor.bindEnvironment(function callback(datum) { publishHandler.changed(/* ... */, datum); })); //Alternatively with Meteor.setTimeout: Meteor.setTimeout(function callback() { publishHandler.changed(/* ... */, 'someData'); }, 10000); 

Since this is after all a JS object, you can also store it in an array or do whatever suits you.
Asynchronous.
Heroically.

+4
source share

All Articles