Meteor methods - Promise on server side

Hey,
assuming I’m having the following Meteor method:

server/methods.js

'test': function() {

    return new Promise((resolve) => {
       setTimeout(() => {resolve(true);},10000);
    });
}

server/test.js

Meteor.call('test',(err,res) => {console.log(res)});

The problem is, that the callback is called immediately without waiting for the Promise. As response I only get a {}.If I do the same on client side

client/test.js

Meteor.call('test',(err,res) => {console.log(res)});

everything is working fine. Why does a Promise method don’t work on server side? I’ve already tried https://github.com/deanius/meteor-promise/, but this one is also not working (I guess it’s client side only if I take a look into the package.js).

Is there any workaround or package? Currently I have to wrap our async function with Meteor.wrapAsync but it would be cool if I could remove it and replace it with some ES6 stuff :slight_smile:

2 Likes

Hey,

You should have a look here

1 Like

@n1s Yeah, I’ve read this great article already. The problem is that it shows the typical client<->server case where Promises work fine. In my case, I do a server<->server method call - there I get an empty response within the callback.

@robfallows any idea why server<->server doesn’t work (example above)?

@XTA you can try const promise = someFunctionThatReturnsAPromise(); return promise.await(); similar to what i posted here: Nightmarejs inside meteor Can't return method result

Let me know if it works for you.

3 Likes

You can use the synchronous version of Meteor.call and use Promise.await(...) to make your promise synchronous

3 Likes

@tomsp Thank you man, this way it works :slight_smile:

1 Like

I discovered this behaviour a while ago, but didn’t make a repro. I’m going to do that today and raise an issue if needed.

EDIT: Issue raised: