Skip to content

Can I still use Meteor.call?

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.

How to migrate from Meteor.call to Meteor.callAsync

Example of how to migrate from Meteor.call to Meteor.callAsync:

js
import { Meteor } from "meteor/meteor";

let data, error;

Meteor.call("getAllData", (err, res) => {

  if (err) {
    error = err;
  } else {
    data = res;
  }
});

// render data or error
js
import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";

const MyCollection = new Mongo.Collection("myCollection");

Meteor.methods({
  getAllData() {
    return MyCollection.find().fetch(); 
  },
});
js
import { Meteor } from "meteor/meteor";

try {
  const data = await Meteor.callAsync("getAllData"); 
  // render data
} catch (error) {
  // render error
}
js
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(); 
  },
});

Considerations for Effective Use of Meteor.callAsync

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:

  • fetch/XMLHttpRequest
  • setTimeout or setImmediate
  • indexedDB
  • web workers
  • any other async web api's that wait on macrotasks

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