callAsync alternatives #12254
Replies: 6 comments 1 reply
-
I don't get this part. Is it actually possible for it to hang indefinitely? I thought the worse thing is an incorrect state, i.e., a false positive error. |
Beta Was this translation helpful? Give feedback.
-
if you can't call a stub while a stub is running, there should be a reactive data source you can query to find out if a stub is running. |
Beta Was this translation helpful? Give feedback.
-
and
Is there a reason those common scenarios were overlooked? Is fixing this on the roadmap? |
Beta Was this translation helpful? Give feedback.
-
Hi, all this was a pretty early development. I recently fixed the tests inside the ddp-client package and encountered a problem around this. After changing some things, now we can call callAsync like this: if (Meteor.isServer) {
const coll = new Mongo.Collection('test');
Meteor.methods({
async insertData(data) {
const id = await coll.insertAsync(data);
console.log('inserted with: ', id);
return id;
},
async updateData(id, data) {
console.log('before update:', await Meteor.callAsync('getData', id));
const r = await coll.updateAsync(id, { $set: data });
console.log('after update:', await Meteor.callAsync('getData', id));
return r;
},
async getData(id) {
const data = await coll.findOneAsync(id);
await coll.updateAsync(id, { $set: { gotData: true } });
return data;
},
});
}
if (Meteor.isClient) {
Meteor.callAsync('insertData', {a: 1})
.then(async result => {
const id = await result.stubValuePromise;
return Meteor.callAsync('updateData', id, {a: 2});
})
.then(async result => {
const count = await result.stubValuePromise;
console.log('update result', count);
});
} The results are:
As you can see, in the server I have In the client, the only difference is that I receive an object with a This is not available yet, but it'll be in the first alpha. As for the why we have to do like this, it'll be explained in the docs. This may not be the final version, so it can change. But the good news is that it worked and we don't need to worry about queues and stuff. |
Beta Was this translation helpful? Give feedback.
-
Hello there, is there an update to this issue? We have started to asyncify our code and encounter many methods, which call methods, because it was much easier this way while implementing. Moving everything into Meteor.isServer as @denihs proposed would mean to leave the simulation feature of meteor behind, which would be a pity (smooth loading). |
Beta Was this translation helpful? Give feedback.
-
Right now, we're moving with the solution on this PR. Improvements will be made in the future if needed, of course! |
Beta Was this translation helpful? Give feedback.
-
I was reading about the new
Meteor.callAsync
in Meteor 2.8, and specifically #12196 (comment) by @zodern, which I strongly agree with.First I'd like to argue that the current behavior of
Meteor.callAsync
is extremely difficult to work with. It's pretty common to trigger a method call (with stubs) on the client in an event handler. A second event could easily happen while the previous method call is still running. And this is now invalid. I actually can't think of a situation where I could safely use the currentMeteor.callAsync
without writing my own queue system. This seems like we're making the common case much more difficult.I have a suggestion for instead making recursive
Meteor.call[Async]
(where one method gets called during the execution of another method stub, something I do do but rarely) more difficult but still possible:Meteor.callAsync
provides to the method function a handle to identify the call.this
already does this.Meteor.callAsync
. Actually,this.callAsync(...args)
would probably make the most sense, orMeteor.callAsync.call(this, ...args)
.Then the default meaning of
Meteor.callAsync
(without specifying the parent) would be to start a new call, which adds it to a global queue. If a user accidentally attempts a recursive call this way, the queue would hang, so the mistake would (hopefully) be clear. (We could add logs after a timeout to help debug as well.) But by providing the parent, it's understood that this is a recursive method call within a method stub, so this is still possible (just slightly harder).Beta Was this translation helpful? Give feedback.
All reactions