Hi, I am looking for some help on waiting for two Asynchronous calls to complete on the client.
Background: I am looking to merge two list of users client side (on Cordova):
- A basic user list which I need to subscribe to on the server
- the Cordova contacts list which I need as per a find call, as per
navigator.contacts.find(fields, onSuccess, onError, options);
I’d like to show these two lists merged into a single list. As Cordova is client-side only, this has to happen on the client.
I can do the Meteor.subscribe
and wait for the onReady
call back and then (nested in onReady
) try and do the same for the navigator.contacts.find
call but I am struggling with getting things to wait for them to complete before combining the lists for rendering.
My plan was to use Meteor.wrapAsync
client side. My first try of trying to get the client to wait for the subscribe to complete does not seem to work. Here’s what I have on the client:
aFindAsyncContacts = function (query, callback){
Meteor.subscribe('searchContacts', query, {
onReady: function(){
console.log("searchContacts READY !!!!!!!!!!");
callback && callback(null);
},
onStop: function(err){
console.log("searchContacts Problem !!!!!!!!!!" + err);
callback && callback(err);
}
})
}
var waitForContacts = Meteor.wrapAsync(aFindAsyncContacts);
var result = waitForContacts(query);
console.log("result of async:" + result + " ABOUT TO RETURN CONTACTS")
Here’s the console out put that shows that the wrapAsync is not waiting for the subscribe to complete:
result of async:undefined ABOUT TO RETURN CONTACTS
selectize_helpers.js?d05c8975297110cd0ad43631f5bbeffce245ce1c:196 about to return []
selectize_helpers.js?d05c8975297110cd0ad43631f5bbeffce245ce1c:138 searchContacts READY !!!!!!!!!!
Any guidance appreciated