XTA
February 12, 2017, 4:04am
1
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
2 Likes
n1s
February 12, 2017, 10:13pm
2
Hey,
You should have a look here
There have been a number of forum posts recently around the topic of working with Promises in Meteor.
I’ve put together a short Medium article on server-side Promises and how they interact with Meteor’s classic Fiber-based coding, including mixing 3rd party Promises with Meteor Promises.
I’m not 100% sure that Medium is the best tool for technical articles, but here it is anyway. Feedback welcomed!
1 Like
XTA
February 12, 2017, 10:37pm
3
@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)?
tomsp
February 13, 2017, 8:09am
4
@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
n1s
February 13, 2017, 8:34am
5
You can use the synchronous version of Meteor.call
and use Promise.await(...)
to make your promise synchronous
3 Likes
XTA
February 13, 2017, 11:00am
6
@tomsp Thank you man, this way it works
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:
opened 11:32AM - 14 Feb 17 UTC
closed 12:37PM - 20 Mar 17 UTC
Refer to [this post](https://forums.meteor.com/t/how-to-declare-async-function-m… yfunction-inside-meteor-methods-so-that-front-end-can-call-it/25991/2) on the Meteor forums and to @benjamn's comments which follow on.
In a nutshell, a `Meteor.call` done on the server does not get the return value of an async method. Instead, it gets the Promise object. This is at variance with the expected behaviour.
This has been observed since v1.3, but the reproduction uses v1.4.2.6:
Reproduction:
git clone https://github.com/robfallows/promise-issue promise-issue
cd promise-issue
meteor npm i
meteor
The server's console log shows the result of calling the async method (shows `{}` - expected "42"):
I20170214-11:23:14.415(0)? The answer to the ultimate question of life, the universe and everything is ... {}
The client browser shows the result of calling the async method from the client (shows "42" as expected):
The answer to the ultimate question of life, the universe and everything is ... 42