Can I return a promise from a Meteor method?


#1

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);
      });
    });
  }
});

#2

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

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