Meteor js parallel db fetch. -
let's have code fetches data multiple collections follows.
//code running on server. var = collectiona.findone({...}); var b = collectionb.findone({...}); var c = collectionc.findone({...}); var d = collectiond.findone({...}); if not wrong above code run in serial fashion. waiting time fetch collection add , and response time delayed.
is there way run above code in parallel fashion, preferably promise pattern?
meteor not use promise or async, uses fibers. use directly or via methods.
with method this:
serverside:
function fetchafromdb(arg,cb){ //function needs callbac var = collectiona.findone(arg); if (!a) { cb(new meteor.error("a-not-found", "can't find a")); else cb(null,a) //first argmument error } meteor.methods({ fetcha: function (arg) { var fetchaasync = meteor.wrapasync(fetchafromdb); //wrap function async var result = fetchaascync(arg); //execute function return result; //return value } } //call method on server //async meteor.call('fetcha', {_id:'foo'}, function (error, result) { ... } ); //blocking var result = meteor.call('fetcha', {_id:'foo'}); call clientside with:
meteor.call('fetcha', {_id:'foo'}, function (error, result) { ... } ); another way use fiber directly (documentation fibers):
for use-case that:
serverside:
function dosomethingwith(a){...} //does var fiber = npm.require('fibers'); var async = fiber(function() { var = collectiona.findone({...}); return dosomethingwith(a); }); //calls async function async.run();
Comments
Post a Comment