Can I return a promise from a Meteor method?

I haven’t written Meteor methods in a while, and I remember having to use Fibers if I wanted to call an async function from a method and return the value from the async function. Is that still the case, or can I do something like the following?

Meteor.methods({
  'items.search'(term) {
    return new Promise((resolve, reject) => {
      asyncFunctionWithCallback(term, (data) => {
        resolve(data);
      });
    });
  }
});
1 Like

Yes - the Promise is resolved before the data is sent over the wire.

It’s also ok to write async methods:

Meteor.methods({
  async myMethod() {
    const a = await someAsyncFunction();
    return await someOtherAsyncFunction(a);
  }
});
3 Likes

Unfortunately, the async function is part of another library, so it needs a callback. Thanks for the answer!

1 Like