In Javascript, (a node environment if that helps), I have a few functions that require to query different databases and use the combined data. Since they are all asynch, it makes for some really messy nested code (for example)
database1.find(options, function(docs){
docs1 = docs;
database2.find(options, function(docs){
docs2 = docs;
database3.find(options, function(docs){
docs3 = docs;
//do things with docs1, docs2, docs3
});
});
});
Is there a way to write a wrapper (or maybe a library already exists) that would let me write something to call the function, and block until it returns with the requested value?
docs1 = waitUntilDone(database1.find(options, function(docs)));
docs2 = waitUntilDone(database1.find(options, function(docs)));
docs3 = waitUntilDone(database1.find(options, function(docs)));
//do things with docs1, docs2, docs3