Is it possible to use promises with Validated Methods and still return the stub value?

I’ve tried wrapping the Validated Method .call in a promise which lets me move away from callback syntax but it looks like I don’t receive the stub value in the client anymore. Is there a way to use promises and return the stub value?

// callbacks syntax - works as expected
function submit() {
  // todoId is available immediately from stub value
  const todoId = insert.call({text: newTodo}, error => { 
    return alert(error)
  });
}

// wrapping the Validated Method in a promise and using async / await syntax
// this works but I don't get the stub value
// if I don't wrap Validated Method in a promise, I get the stub value but the error doesn't appear to be caught in the catch block
async function submit() {
  try {
    const todoId = await insert.call({text: newTodo}) // no stub value, only get todoId after server has returned
    console.log('todoId', todoId); 
  } catch (error) {
    return alert(error)
  }

When returning a promise from your validated method something like this is possible:

insert.call({ text: newTodo })
    .then(todoId => {
        console.log('todoId', todoId);
    })
    .catch(err => alert(err));

Unfortunately it looks like I can’t get the stub value using the .then / .catch syntax either. Using the callback syntax I can get the stub value immediately. I was hoping to move away from the callback syntax and still get the stub value without having to wait for the server to return.

I’ve written a more elaborate case study on what we did in this topic: Using ValidatedMethod in Meteor 2.8+

We can retrieve document Id’s just fine on our end. Perhaps this section of the documentation can shed some light on your problem: GitHub - meteor/validated-method: Meteor methods with better scoping, argument checking, and good defaults.

1 Like