Is there any way to use return statement twice in meteor method?
Meteor.methods({
getData() {
let i = 0;
while(i <= 2) {
return 'check ’ + i
i++;
}
}
})
Currently, This method sends response only one time. Can we modify this method to send response twice? Is there any way to do this. (without calling the method multiple time)
In the method, I’m doing the 3rd party API calls multiple times to fetch the data according to the entity. and I just want to know that Is there any way to using return statement multiple times, to return the response when we get data from 1st API.
when they’re both on client everything works. but when I tried to use the generator as a method
(eg "const getLearn = Meteor.call(‘printlog’)),
I got err msg “Cannot read property ‘next’ of undefined”. that also happened when I put the logs inside a callback of Meteor.call.
What am I missing?
Thanks in advance
I called it 3 times, each time inside the “if” of previous call. it logged “1” 3 times.
and it makes sense, because each time we call the method, it calls the generator from fresh…
Came in here to potentially suggest two of the three options that @robfallows did - did not even think to use generators (who knew they had a practical use case!!*).
Mind blown, cheers for the lesson!
Still failing to see why multiple returns would be useful in a practical setting
Will every user have it’s own Meteor.methods instance? Or is it a shared global? I don’t think methods are bound to the request context / socket instance? Or are they?
Especially in the code sample you’re providing, as const g = gen(); is clearly on the global scope.
That was never intended as a definitive solution, just a demonstration. The way to do it properly is to use the connection id inside the method as an index to an array of generators. A new connection instantiates a generator. On disconnect, the instance is destroyed, to prevent memory leaks.
I’m not at my computer, so can’t show the code atm.
You can’t create the method itself as a generator and then initialize it from the client (which is a pity).
you should define & initialize the generator in the server side, and call the initialized instance from the method, and then call the method from the client few times, as much as the number of yields in the generator.
To avoid multy-users problems, you should establish a system to create a different instance for every use of the method, and manage it in array/dict.
If multiple users need to share potentially changing data, it sounds like you need to use a database!
Your method should be as simple as:
getNext() {
Meteor.defer(() => {
// Check if we're refreshing by updating a single "control" mongo document and see if anything changes
const refreshing = ControlCollection.update({_id: 'control', refreshing: false}, {$set: {refreshing: true}}) === 1
if (refreshing) { return; }
// If we aren't, start fetching the latest data from whatever http stuff you want and insert it
// into a database the client is subscribed to
// We can now allow somewhere else to do the refresh
ControlCollection.update({_id: 'control', refreshing: true}, {$set: {refreshing: false}})
})
}
getNext() will return immediately. Update a timestamp in the collection to indicate the freshness of the data. Never return data in methods!