You’re using the arrow function syntax incorrectly by also typing the function keyword.
My Bad! Still not familiar with the new js. Thanks! It works!
How I can manage the following?
- When Meteor.call for validateObject is triggered
- Waterfall goes thru the callbacks
- At last the final result of the waterfall is returned to the Meteor.call via Meteor.method function and printed into console.log
This would be super useful
var async = require("async");
var waterfall = require("async/waterfall");
Meteor.methods({
'validateObject': function(object) {
async.waterfall([
function(callback) {
// run first
callback(null, result);
},
function(result, callback) {
// run second
callback(null, result);
}
], function(error, result) {
// at the end provide return to Method 'validateObject';
});
}
});
var object = {_id: "string", name: "string"}
Meteor.call("validateObject", object, function(error, result){
if(result) {
// Display the return from Method 'validateObject';
console.log(result);
}
});
Hello @trusktr I am little confused about using async/await in meteor method in server side
I am trying this code, but doesn’t works:
const requestWithPromise = async (options) => {
// Axios create a promise to return request result: https://www.npmjs.com/package/axios
const result = await axios(options);
console.log('showing axios result', result.data);
return result.data;
};
Meteor.methods({
'Request.execute'(data) {
const options = {method: 'post', url, data, timeout};
const result = requestWithPromise(options);
console.log('showing myPromise result' , JSON.stringify(result));
return result;
}
});
In my meteor method I should wait for request result, but the log order is the following:
showing myPromise result {}
showing axios result {id:.............}
You’re missing the await
:
const result = await requestWithPromise(options);
Hi @sashko, I can’t use await inside a meteor method, because I should define the method as async and it generates error, but I fixed my problem with a simple thing:
'Request.execute'(data) {
const options = {method: 'post', url, data, timeout};
const response = Promise.await(axios(options));
console.log('SHOWING ASYNC' , JSON.stringify(response.data));
return response.data;
}
I am working with Meteor@1.4.2.
This is the required package https://github.com/meteor/promise
Yes - this sort of thing is supported:
Meteor.methods({
async someMethod(x, y) {
const a = await someAsyncFunction(x);
return await someOtherAsyncFunction(a, y);
}
});
@robfallows have you actually done that? Based on what @ilacyero reports:
… I can’t use await inside a meteor method, because I should define the method as async and it generates error …
Yes. It works exactly as it should when called from the client (the Promise is resolved before the result is sent back over the wire).
When I was playing with this a few months back I got unexpected results (the Promise itself) when calling an async method from within the same server code (not something I normally do).
Hm, so given a methods-calling-methods situation, eventually some of those methods will be getting called form the server as well. And then it might (based on what I understand from your experience) cause issues, right?
Perhaps. I found that behaviour, but I haven’t revisited those tests for quite a while, so it may be fixed now. I should probably take another look. I was putting a repro together for @benjamn, but got sidetracked and didn’t finish it.
Hm, thank you, though, I guess I’ll need to find that out for myself sooner or later and I’ll sure do post back here if/when I do.
Hmm, interestingly what I did which works is I just returned the promise in the method and then in the Meteor call all I did was await, seems a lot simpler than what you’re doing here and works for me but maybe there’s a reason why I shouldn’t do this?
I just trial and errored here, maybe this is not the right approach
Let’s say you have a call on the client like following
Meteor.call('myMethod', (err, res) => alert(res))
with an async function you can define the method like that:
const myPromise = () => new Promise((resolve, reject) => resolve('hi'))
Meteor.methods({
myMethod: async () => {
const msg = await myPromise()
return msg
}
})
This essentially allows to write your code in a “synchronous” style, while still staying async by await
ing promises.
sorry @robfallows but it doesn’t. Async/Await stopped working a few months ago (this summer?). Now I have to use Promise.await().
It works in NPM modules, but as soon as the async is somewhere near client side code, it stops working.
I opened a few topics and filed a github issue, but I got no response.
Do you have a repro? I’ve been using async/await on the client and server without issue (other than in-server method calls as mentioned). I’m currently on 1.4.2.
The important thing to remember is that if you “promisify” a Meteor method, the Promise is resolved before the data is sent over the wire to the client. That means that you can, if you wish, continue to use the standard, callback form of Meteor.call
and it will work as it always did.
However, you can “promisify” the call. I use something like this for a promised call:
const callWithPromise = (method, myParameters) => {
return new Promise((resolve, reject) => {
Meteor.call(method, myParameters, (err, res) => {
if (err) reject('Something went wrong');
resolve(res);
});
});
}
and then inside an async
function:
const result = await callwithPromise('someMethod', { some: parameter, someOther: parameter });
Hmm, interestingly what I did which works is I just returned the promise in the method and then in the Meteor call all I did was await, seems a lot simpler than what you’re doing here and works for me but maybe there’s a reason why I shouldn’t do this?
I just trial and errored here, maybe this is not the right approach
@robfallows using your example just above, can you show an example of “chaining” promises too?
Chaining async calls using promises to me is the real value here.