v3-docs/v3-migration-docs/breaking-changes/call-x-callAsync.md
You can, but we recommend you use it only to call methods that do not have a method stub, or when the method stub is synchronous.
In fact, we log a warning message if you use Meteor.call to call a method with an async method stub since it can lead to unexpected behavior.
Meteor.callAsync is the standard for calling methods and supports any method, including those that have an async method stub.
Here is also important to remember what a stub is. A stub is a client-side simulation of the server-side method that runs immediately when the method is invoked, allowing the client to update its state optimistically before receiving the server's response. So, basically any Meteor method that is defined on the client is considered a stub.
Example of how to migrate from Meteor.call to Meteor.callAsync:
::: code-group
import { Meteor } from "meteor/meteor";
let data, error;
Meteor.call("getAllData", (err, res) => {
// [!code highlight]
if (err) {
error = err;
} else {
data = res;
}
});
// render data or error
import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";
const MyCollection = new Mongo.Collection("myCollection");
Meteor.methods({
getAllData() {
return MyCollection.find().fetch(); // [!code highlight]
},
});
import { Meteor } from "meteor/meteor";
try {
const data = await Meteor.callAsync("getAllData"); // [!code highlight]
// render data
} catch (error) {
// render error
}
import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";
const MyCollection = new Mongo.Collection("myCollection");
Meteor.methods({
async getAllData() {
return await MyCollection.find().fetchAsync(); // [!code highlight]
},
});
:::
When we introduced async Method stubs the implementation brought some limitations.
Those limitations were addressed in this package created by Zodern, and later, we moved the solution to the core.
But there is no perfect solution to the problems with async stubs.
To ensure other code will not run while an async stub is running, async stubs can not use these API's:
Using these API's could allow other code to run before the async stub finishes.
If one of these API's are used, a warning will be shown in the console:
Method stub (<method name>) took too long and could cause unexpected problems. Learn more at https://v3-migration-docs.meteor.com/breaking-changes/call-x-callAsync.html#what-are-the-limitations-of-call-meteor-callasync